Skip to main content
deleted 43 characters in body; edited tags; edited title
Source Link
200_success
  • 145.6k
  • 22
  • 191
  • 481

Upload a file Asynchronouslyasynchronously

I need your opinion about my practice,. I want to improve it.

The requirements are the following:

  • RESTFUL + OWIN (SelfHosted)
  • Endpoint - Upload a file asynchronously
  • Make a request to the endpoint from Postman

Well, This is what I got

For the OWIN selfhosted I followed the Microsoft Documentation

OWIN Self-host

Endpoint:

FileController.cs

        [HttpPost]
        [Route("api/v1/upload")]
        public HttpResponseMessage Post([FromUri]string filename)
        {
            var task = this.Request.Content.ReadAsStreamAsync();
            task.Wait();
            Stream requestStream = task.Result;

            try
            {
                Stream fileStream = File.Create("./" + filename);
                requestStream.CopyTo(fileStream);
                fileStream.Close();
                requestStream.Close();
            }
            catch (IOException)
            {
                throw new HttpResponseException( HttpStatusCode.InternalServerError);
            }

            HttpResponseMessage response = new HttpResponseMessage();
            response.StatusCode = HttpStatusCode.Created;
            return response;
        }

Postman:

The postman returns Status Code 201

Some feedback please

Regards.

Upload a file Asynchronously

I need your opinion about my practice, I want to improve it.

The requirements are the following:

  • RESTFUL + OWIN (SelfHosted)
  • Endpoint - Upload a file asynchronously
  • Make a request to the endpoint from Postman

Well, This is what I got

For the OWIN selfhosted I followed the Microsoft Documentation

OWIN Self-host

Endpoint:

FileController.cs

        [HttpPost]
        [Route("api/v1/upload")]
        public HttpResponseMessage Post([FromUri]string filename)
        {
            var task = this.Request.Content.ReadAsStreamAsync();
            task.Wait();
            Stream requestStream = task.Result;

            try
            {
                Stream fileStream = File.Create("./" + filename);
                requestStream.CopyTo(fileStream);
                fileStream.Close();
                requestStream.Close();
            }
            catch (IOException)
            {
                throw new HttpResponseException( HttpStatusCode.InternalServerError);
            }

            HttpResponseMessage response = new HttpResponseMessage();
            response.StatusCode = HttpStatusCode.Created;
            return response;
        }

Postman:

The postman returns Status Code 201

Some feedback please

Regards.

Upload a file asynchronously

I need your opinion about my practice. I want to improve it.

The requirements are the following:

  • RESTFUL + OWIN (SelfHosted)
  • Endpoint - Upload a file asynchronously
  • Make a request to the endpoint from Postman

Well, This is what I got

For the OWIN selfhosted I followed the Microsoft Documentation

OWIN Self-host

Endpoint:

FileController.cs

    [HttpPost]
    [Route("api/v1/upload")]
    public HttpResponseMessage Post([FromUri]string filename)
    {
        var task = this.Request.Content.ReadAsStreamAsync();
        task.Wait();
        Stream requestStream = task.Result;

        try
        {
            Stream fileStream = File.Create("./" + filename);
            requestStream.CopyTo(fileStream);
            fileStream.Close();
            requestStream.Close();
        }
        catch (IOException)
        {
            throw new HttpResponseException( HttpStatusCode.InternalServerError);
        }

        HttpResponseMessage response = new HttpResponseMessage();
        response.StatusCode = HttpStatusCode.Created;
        return response;
    }

Postman:

The postman returns Status Code 201

Source Link

Upload a file Asynchronously

I need your opinion about my practice, I want to improve it.

The requirements are the following:

  • RESTFUL + OWIN (SelfHosted)
  • Endpoint - Upload a file asynchronously
  • Make a request to the endpoint from Postman

Well, This is what I got

For the OWIN selfhosted I followed the Microsoft Documentation

OWIN Self-host

Endpoint:

FileController.cs

        [HttpPost]
        [Route("api/v1/upload")]
        public HttpResponseMessage Post([FromUri]string filename)
        {
            var task = this.Request.Content.ReadAsStreamAsync();
            task.Wait();
            Stream requestStream = task.Result;

            try
            {
                Stream fileStream = File.Create("./" + filename);
                requestStream.CopyTo(fileStream);
                fileStream.Close();
                requestStream.Close();
            }
            catch (IOException)
            {
                throw new HttpResponseException( HttpStatusCode.InternalServerError);
            }

            HttpResponseMessage response = new HttpResponseMessage();
            response.StatusCode = HttpStatusCode.Created;
            return response;
        }

Postman:

The postman returns Status Code 201

Some feedback please

Regards.