Upload files to Asp.NET MVC 4 - Web API - Http Put Method

Asked

Viewed 101 times

0

So, I have to do a method in my controller for an API that accepts 2 files via PUT (1 json E1 xml for data processing, not to save the files), and I have to send the request via Fiddler.. My request in Fiddler is as follows (PUT method):

Content-Type: multipart/form-data
Authorization: XXXXX 
Host: XXXX

Request Body:

<@INCLUDE *C:\Users\ZZZZ.json*@>
<@INCLUDE *C:\Users\XXXX.xml*@>

Here is the code of my controller so far (does not work)

[HttpPut, Route("api/Student/{studentId}/Classes/{classId}")]
        public async Task<string> Put(int studentId, int classId)
        {
                var file = HttpContext.Current.Request.Files.Count > 0 ?
            HttpContext.Current.Request.Files[0] : null;
            Stream fileContent = await this.Request.Content.ReadAsStreamAsync();
            MediaTypeHeaderValue contentTypeHeader = this.Request.Content.Headers.ContentType;

            if (fileContent != null)
                return "ok";
            return "not ok";
        }

So far the file is not uploading ne, it appears in the "Request" variable. I also do not have the Request. I also tried Httpcontext and also had nothing.

I tried exactly the same thing but with the POST method (it automatically adds boundaries to the Fiddler request, and then I can see the file names on my controller, but I can’t read any of them...

What would you do to make it work? I really have to have a json object and a different xml object... Files have a dynamic structure, that is, it is not worth creating a specific object for them.

PS : How would you then read the same files without saving them to disk first?

1 answer

0

Browser other questions tagged

You are not signed in. Login or sign up in order to post.