Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6854745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:40:19+00:00 2026-05-27T01:40:19+00:00

I have a problem reading uploaded XML file to the string instead of file.

  • 0

I have a problem reading uploaded XML file to the string instead of file.

My problem is that when I try to access the stream (var stream = part.ContentReadStream) then it’s closed. I have feeling that it is accessing closed file stream. Am I using MultipartFormDataStreamProvider incorrectly? File size is only few kilobytes, so that should not be a problem.

    [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>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T01:40:20+00:00Added an answer on May 27, 2026 at 1:40 am

    The solution was actually quite simple. MultipartFormDataStreamProvider is not required when we are not dealing with files. This works quite smoothly on my case.

    [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;
    }  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem while reading from XML file in action script 3. This
I have a Java problem that involves reading a text file and check that
I have a problem reading a txt file to insert in the mysql db
I have a problem reading OWL/XML files from Java using Jena. I have no
I have a problem with reading empty string in C. I want to read
I have a problem reading data file: /// * ABC Names A-06,B-18, * Data
I have a problem reading a comma-delimited TXT file. This is what I am
Hi friends hope all r doing well. I have a problem while reading xml
Hi friends hope all r doing well. I have a problem while reading xml
I have a problem when using simplexml to read a xml document that I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.