14

I would like to know how best to handle file upload and addtional information added to the file to be uploaded using ASP.NET Web API 2 without MVC components. I have google the net and I can tell you I am more confused than I expected.

The Additional info will be stored in db and the file on the disk. So far the Web API app I am building does not support multipart/form-data. It only supports the default media types. I know I need to create a media formatter.

Pls help.

3
  • 6
    Check out this link from the ASP.NET site: Sending HTML Form Data: File Upload and Multipart MIME Commented Apr 3, 2014 at 11:17
  • Can't you just upload the file and send extra data in the querystring? I know it's ugly, but if the app does not support multipart/form-data, that's probably what you'll get Commented Feb 21, 2017 at 18:32
  • Here is link how to handle this in MVC, you may find something helpful prideparrot.com/blog/archive/2012/8/… Commented Sep 21, 2017 at 10:51

2 Answers 2

1

I had wrote Javascript split File and upload to WEB API . i think you can reference my backend codes

In front-end you need using below code to upload your File

  var xhr = new self.XMLHttpRequest();
  xhr.open('POST', url, false);
  xhr.setRequestHeader('Content-Type', 'application/octet-stream');
  xhr.send(chunk);

In backend use Request.InputStream.Read to catch your file bytes

    [HttpPost]
    [ValidateInput(false)]
    public string fileUpload(string filename)
    {
        byte[] file = new byte[Request.InputStream.Length];
        Request.InputStream.Read(file, 0, Convert.ToInt32(Request.InputStream.Length));
        BinaryWriter binWriter = new BinaryWriter(new MemoryStream());
        binWriter.Write(file);
        StreamReader reader = new StreamReader(binWriter.BaseStream);
        reader.BaseStream.Position = 0;
        //This example is recevied text file
        while ((line = reader.ReadLine()) != null)
        {

         };
    }
Sign up to request clarification or add additional context in comments.

Comments

0

You can just serialize your file data into BASE64 and send them as a string in case of multipart/from-data is not allowed for some reason.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.