从一个多/ form-data发布读取文件输入 [英] Reading file input from a multipart/form-data POST

查看:1083
本文介绍了从一个多/ form-data发布读取文件输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过HTML表单提交文件到WCF REST服务,与是enctype 设置为的multipart / form-data的和单个组件:<输入类型=文件名称=数据> 。由服务器读取所得流包含以下内容:

I'm POSTing a file to a WCF REST service through a HTML form, with enctype set to multipart/form-data and a single component: <input type="file" name="data">. The resulting stream being read by the server contains the following:

------WebKitFormBoundary
Content-Disposition: form-data; name="data"; filename="DSCF0001.JPG"
Content-Type: image/jpeg

<file bytes>
------WebKitFormBoundary--

问题是,我不知道如何从数据流中提取文件的字节。我需要这样做是为了将文件写入到磁盘中。

The problem is that I'm not sure how do extract the file bytes from the stream. I need to do this in order to write the file to the disk.

推荐答案

您可以看看的的以下博客帖子这说明可以用来解析的multipart / form-data的在服务器上的技术使用多部分解析器

You may take a look at the following blog post which illustrates a technique that could be used to parse multipart/form-data on the server using the Multipart Parser:

public void Upload(Stream stream)
{
    MultipartParser parser = new MultipartParser(stream);
    if (parser.Success)
    {
        // Save the file
        SaveFile(parser.Filename, parser.ContentType, parser.FileContents);
    }
}

另一种可能性是使<一href=\"http://stackoverflow.com/questions/1716868/parsing-multipart-form-data-in-net-c/1718859#1718859\">aspnet兼容性,并使用 HttpContext.Current.Request 但是这并不是一个非常WCFish方式。

Another possibility is to enable aspnet compatibility and use HttpContext.Current.Request but that's not a very WCFish way.

这篇关于从一个多/ form-data发布读取文件输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆