-1

I need to pass an Excel file in to and web service implemented in C#. The web service I have created so far is as follows.

 public void loadData([FromBody]string dataFile)
        {

        }

I also thought that it is accepatble to pass the Excel file is using a Base64 string; Is this a good way or are there any better ways to do this?

DafaDil
  • 2,463
  • 6
  • 23
  • 33

1 Answers1

0

Basically, you pass the file as part of a multipart/form-data HTTP request.

How To Accept a File POST

Sending HTML Form Data in ASP.NET Web API: File Upload and Multipart MIME

Edit: To receive a binary file of any type, use the following code. However, this is fairly unsafe. Really, the request headers should be checked for content type and content length.

public void loadData() {
    /* get raw data */
    string dataFile = Request.Content.ReadAsStringAsync().Result
}

Accepting Raw Request Body Content with ASP.NET Web API

Community
  • 1
  • 1
Alex
  • 5,909
  • 2
  • 35
  • 25
  • will the web service still be the same? I am referring to the public void loadData([FromBody]string dataFile) – DafaDil Dec 03 '14 at 21:03
  • In my opinion, it's not a good idea to pass a file as a string. You don't have any idea what the type the file is, or what the actual binary length is. Also, mutlipart/form-data content type is the only way for HTML forms to submit files. Instead of mutlipart/form-data or base64 string, you can just send the file binary. http://blog.marcinbudny.com/2014/02/sending-binary-data-along-with-rest-api.html#.VH986zE73Fk – Alex Dec 03 '14 at 21:12
  • so what must be the signature of the method public void loadData(?) if using mutlipart/form-data content type – DafaDil Dec 03 '14 at 21:16
  • Edited the answer. It now includes links to do both ways. – Alex Dec 03 '14 at 21:27
  • I have the following check if (!Request.Content.IsMimeMultipartContent()) { throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType); } But the string dataFile (Request.Content.ReadAsStringAsync().Result) is empty – DafaDil Dec 03 '14 at 21:37