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

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

问题描述

我正在通过 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 的技术="http://multipartparser.codeplex.com/" rel="noreferrer">多部分解析器:

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);
    }
}

另一种可能性是启用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.

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

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