2

How do I send an excel file coming from file upload input to my ASP.NET WebAPI and then save that excel file so I can read its data?

Here's what I've got (button click calls upload()) - just the basics, which works fine:

    function upload() {
        $.getJSON("api/uploads/uploadfile",
            function (data) {
                $("#mydiv").append("Success: " + data.Success + " Failed: " + data.Failed);
            });
    }

And my ASP.NET WebAPI method:

    public DBResult UploadFile()
    {
        DBResult result = new DBResult();
        result.Success = 0;
        result.Failed = 0; 

        return result;
    }

Any help is greatly appreciated.

TIA

Rivka
  • 2,172
  • 10
  • 45
  • 74
  • Does that upload work? To upload a file you normally have to actually post a form, e.g. see the [jQuery Form plugin](http://jquery.malsup.com/form/#file-upload), rather than just make a normal AJAX request. And what's the file upload control - an ASP.NET HtmlInputFile control? You should just be able to read the file contents out of the control as a stream. – Rup May 01 '12 at 23:37
  • No, I haven't gotten the file upload to work. I'm using . I've read about form with action and enctype, but I'm still not sure how to retrieve that file in the WebAPI method. – Rivka May 01 '12 at 23:44
  • See my answer here: http://stackoverflow.com/questions/10320232/how-to-accept-a-file-post-asp-net-mvc-4-webapi/10327789#10327789 – Mike Wasson May 02 '12 at 04:57
  • @MikeWasson I was able to figure this out. Your article helped, as well as http://www.strathweb.com/2012/04/html5-drag-and-drop-asynchronous-multi-file-upload-with-asp-net-webapi/. Thanks. – Rivka May 02 '12 at 22:35

2 Answers2

5

I was able to figure this out between these 2 articles:

How To Accept a File POST

http://www.strathweb.com/2012/04/html5-drag-and-drop-asynchronous-multi-file-upload-with-asp-net-webapi/

Community
  • 1
  • 1
Rivka
  • 2,172
  • 10
  • 45
  • 74
1

I posted a similar solution at another question, but using c# to post to webapi:
How To Accept a File POST

Community
  • 1
  • 1
Daniel Melo
  • 161
  • 1
  • 4