MultipartFormDataStreamProvider is creating a blank file even though the multipart form-data request isn't sending one


Scenario: I have a simple Html form which submits multipart form data to a Web API service. The file to be uploaded is optional.

HTML Form:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server"> <title>MultipartFormTest</title> </head> <body>
<form action="http://myhost/WebApplication1/api/SampleTests/UploadFormData" enctype="multipart/form-data"
    What is your name?
    <input type="text" name="submit-name"><br>
    What files are you sending?
    <input type="file" name="files"><br>
    <input type="submit" value="Send">
    <input type="reset">


POST http://myhost/WebApplication1/api/SampleTests/UploadFormData HTTP/1.1
Accept: image/gif, image/jpeg, image/pjpeg, application/x-ms-application, application/vnd.ms-xpsdocument, application/xaml+xml, application/x-ms-xbap, /
Referer: http://myhost/WebApplication1/MultipartFormTest.aspx
Accept-Language: en-us
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; WOW64; Trident/5.0; SLCC1; .NET CLR 2.0.50727; InfoPath.3; .NET4.0C; .NET4.0E; .NET CLR 3.5.30729; MS-RTC LM 8; .NET CLR 3.0.30729)
Content-Type: multipart/form-data; boundary=---------------------------7dc28c251216e4
Accept-Encoding: gzip, deflate
Host: myhost
Content-Length: 301
Connection: Keep-Alive
Pragma: no-cache
Cookie: ASP.NET_SessionId=bm2k3hufxbtezgspkzx1k1qy

Content-Disposition: form-data; name="submit-name"

Content-Disposition: form-data; name="files"; filename=""
Content-Type: application/octet-stream


Expected: File shouldn't be created.
Actual : A blank file is created.

file attachments

Closed Jun 4, 2012 at 6:26 PM by HongmeiG
This is by design because the request contains that segment which has filename="". If you don't want to have the file created, please remove that segment from the request.


trdai wrote May 30, 2012 at 7:23 PM

I believe the actual issue is not about blank content is added to the file body part even though user doesn’t choose a file, after all we can’t control the browser, and with proper client coding, a web page can avoid sending message when user doesn’t choose a file.

The real issue to me is that a zero-length file is created on local machine, which I think we shall avoid.

HenrikN wrote Jun 4, 2012 at 5:32 PM

A zero-length file is a valid file and it does reflect the "empty" body part in the input so I don't see a problem with this.

Content-Disposition: form-data; name="files"; filename=""
Content-Type: application/octet-stream