1

I need to wrap this up but not sure how :-)

Basically, I'm using jQuery forms to upload a file to a WCF REST service.

Client looks like:

<script type="text/javascript">
    $(function () {
        $('form').ajaxForm(
            {
                url: '/_vti_bin/VideoUploadService/VideoUploadService.svc/Upload/theFileName',
                type: 'post'
            })
    });
</script>
<form id="fileUploadForm" name="fileUploadForm" method="post" action="/_vti_bin/VideoUploadService/VideoUploadService.svc/Upload" enctype="multipart/form-data">
  <input type="text" name="filename" />
  <input type="file" id="postedFile" name="postedFile" />
  <input type="submit" id="fileUploadSubmit" value="Do Upload" />
</form>

While server relevant snippets are

[WebInvoke(UriTemplate = "Upload/{fileName}", ResponseFormat = WebMessageFormat.Json)]
void Upload(string fileName, Stream fileStream);

public void Upload(string fileName, Stream stream)
{
   // just write stream to disk...
}

Problem is that what I write to disk looks like this, not the content of the file (in my case, "0123456789"):

-----------------------------7dc7e131201ea
Content-Disposition: form-data; name="MSOWebPartPage_PostbackSource"

// bunch of same stuff here

-----------------------------7dc7e131201ea
Content-Disposition: form-data; name="filename"

fdfd
-----------------------------7dc7e131201ea
Content-Disposition: form-data; name="postedFile"; filename="C:\Inter\Garbage\dummy.txt"
Content-Type: text/plain

0123456789
-----------------------------7dc7e131201ea--

Question - should I be manually parsing what I get above and extract the last part which corresponds to the uploaded file (not elegant)? Or there is a way to apply attribute, content type, whatever setting, in order to get in the stream JUST the uploaded file's content?

I'm suspect there is, but I'm missing it... any help?

Thanks much!

4

1 回答 1

1

看看 2 篇关于使用 ASP.NET Web API 上传表单文件的文章:

http://www.asp.net/web-api/overview/working-with-http/sending-html-form-data,-part-1

http://www.asp.net/web-api/overview/working-with-http/sending-html-form-data,-part-2

ASP.NET Web API 已经有类(如 MultipartFormDataStreamProvider)来解析多部分请求并从中保存数据。为什么不使用这些类来解决您的问题

UPD 如果是 .NET 3.5 代码和托管:

认为 MultipartFormDataStreamProvider 类及其程序集不适用于 .NET 3.5。在这些情况下,您应该使用其他一些库/类来解析多部分或手动进行。

尝试以下项目(MIT 许可证)并从此项目中文件 HttpMultipartParser.cs:

https://bitbucket.org/lorenzopolidori/http-form-parser/src

于 2012-10-07T13:56:18.943 回答