last day (15 days later) » 

10:39 AM
0
Q: File uploads using MultipartFileData results in empty BodyPart files with zero bytes

ProNotionI am implementing file uploads for a web app (.net v4.8) and for reasons I am unable to determine the file uploads result in empty body parts being uploaded, the files all have zero bytes. I have tried testing with small text files and also image files with no success. My code should take the upl...

 
Check receive message to see if there are any multipart attachments which are MIME. A MIME attachments is in the body an starts with two dashes on a new line. See : learn.microsoft.com/en-us/previous-versions/office/developer‌​/…
 
Checking the headers I see Content-Length: 189 Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryseCaFNPPbYd4r53A" - in this instance I was uploading a single text file with a single word within and so the resulting file should be ~1kb
 
The Content length in header is 189 but you said the file should be ~1kb. The content length has to be the same size as the data in the body.
 
I was just giving an example, I can select any size file with the same outcome. The content length is accurate to the file being uploaded.
 
I do not really understand what you code is trying to do. After you Request.Content.ReadAsMultipartAsync(provider); you have to get the Content length and set the header to the size. Then you do not event send the request. All the code seems to do is the read the body of the request before it is sent.
 
10:39 AM
I'm not sure if I am also misunderstanding what you are asking. ReadAsMultipartAsync is reading the request data into the provider. The provider is writing the files to the desired location and then I am renaming them as required. At least that is what is supposed to be happening but it is writing the files with empty contents. What request are you thinking I should be sending?
 
Your code is similar to following : stackoverflow.com/questions/15144469/… You are doing a File.Move which simply moves files. It is not a request. A request has to send data and you do not have any send. A send would look like WebResponse response = request.GetResponse();
 
My code follows the example provided by Microsoft: learn.microsoft.com/en-us/aspnet/web-api/overview/advanced/… and as per the documentation "The MultipartFormDataStreamProvider class is a helper object that allocates file streams for uploaded files. To read the multipart MIME message, call the ReadAsMultipartAsync method. This method extracts all of the message parts and writes them into the streams provided by the MultipartFormDataStreamProvider."
Hopefully, that clarifies what I believe the code achieves. I am passing a directory into the MultipartFormDataStreamProvider and when I call ReadAsMultipartAsync and pass in the provider it writes the message parts to the directory.
 
 
2 hours later…
12:51 PM
Is you code part of the controller on the server or in the client? Is the code process the request from client to server or the response from server to client? You said the message only contained header and not the body (MIME). So where is the code actually sending the message? Not just putting the MIME into the body. This is an upload so client is sending the MIME in the request and server is reading the request.
I really do not see where the client is sending and server receiving. I just looks like you are reading the request at client before it gets sent
 
Sorry if it's not clear. Yes, the code is inside of a Web API Controller.
 
1:30 PM
I think this might be more like a permissions issue after all because I have deployed the change to a remote server inside of the domain of the user account being impersonated and the bodypart is created with content but not renamed but that is a separate issue.
 
If you are getting a message so it is not a permission issue. The client if a connection did not complete would get an exception. You code looks like it is at the server. You are getting the http headers. You need to verify that you have look at the context (body) and see if there is data.
 
1:47 PM
The zero-byte file seems to be a result of the failed renaming of the file. I have commented out the File.Move and the file is created and at the expected size with the expected content.
Thank you for persevering with me until I found the issue.
 
Than issue is at client not adding a file to the multipart. You can use a sniffer like wireshark or fiddler to see the HTTP (not the encrypted HTTPS) to verify the client created the issue not the server.
 
The files are added because they are now being saved on the server as expected
The issue was the line of code attempting to rename from BodyPart_6adf8f87-b813-4dcb-b7c9-74568b67b77d to SomethingUseful.txt
 
Problem solved?
 
Yes, thank you
 

last day (15 days later) »