English 中文(简体)
从多部分文件上读取的内容
原标题:Reading content to the memory from the multi-part file upload

我有问题阅读,把XML文件上载到扼杀,而不是档案。

我的问题是,当我试图进入溪流时(var stream = 部分.ContentReadStream),那就关闭了。 我感到,它正在查阅封闭档案。 Am I 采用多端表格,不正确? 档案尺寸只有很少几公斤,因此不应成为问题。

    [WebInvoke(Method = "POST", UriTemplate = "{importFile}")]
    public HttpResponseMessage Upload(string importFile, HttpRequestMessage request)
    {
        if (!request.Content.IsMimeMultipartContent())
        {
            throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
        }

        // Create a stream provider for setting up output streams
        MultipartFormDataStreamProvider streamProvider = new MultipartFormDataStreamProvider();

        // Read the MIME multipart content using the stream provider we just created.
        var task = request.Content.ReadAsMultipartAsync(streamProvider);
        task.Wait();
        IEnumerable<HttpContent> bodyparts = task.Result;
        string submitter;
        if (!bodyparts.TryGetFormFieldValue("submitter", out submitter))
        {
            submitter = "unknown";
        }

        // Get list of local file names from stream provider
        IDictionary<string, string> bodyPartFileNames = streamProvider.BodyPartFileNames;


        var parser = this.parserFactoryFactory.CreateParser();
        foreach (var part in bodyparts)
        {
            using (var stream = part.ContentReadStream)
            {
                using (var streamReader = new StreamReader(stream))
                {
                    string content = streamReader.ReadToEnd();
                    var results = parser.Parse(content);
                }
            }
        }
        return new HttpResponseMessage(HttpStatusCode.Accepted);
    }  

This is my post

<h3>Data import test</h3>

<form action="/api/data/Upload" enctype="multipart/form-data" method="POST">
    <input type="file" name="importFile"/>
    <input type="submit" value="Upload"/>
</form>
最佳回答

解决办法实际上非常简单。 当我们不处理档案时,不需要多方捐助者。 这在我的案件中非常顺利。

[WebInvoke(Method = "POST", UriTemplate = "{importFile}")]
public HttpResponseMessage Upload(
    string importFile, HttpRequestMessage request)
{
    if (!request.Content.IsMimeMultipartContent())
        throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);

    // Read the MIME multipart content 
    var task = request.Content.ReadAsMultipartAsync();            
    task.Wait();

    IEnumerable<HttpContent> bodyparts = task.Result;
    string submitter;
    if (!bodyparts.TryGetFormFieldValue("submitter", out submitter))
        submitter = "unknown";

    var parser = this.parserFactoryFactory.CreateParser();
    foreach (var part in bodyparts)
    {
        using (var stream = part.ContentReadStream)
        {
            using (var streamReader = new StreamReader(stream))
            {
                string content = streamReader.ReadToEnd();
                var results = parser.Parse(content);
                if (results.IsValid)
                    // do something
            }
        }
    }
    var message = new HttpResponseMessage(HttpStatusCode.Accepted);
    return message;
}  
问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签