Link to home
Start Free TrialLog in
Avatar of Dinesh Kumar
Dinesh KumarFlag for India

asked on

File Upload on Server through a webservice

Hi, I want through following service and tested it also using Fiddler.

http://blog.anthonybaker.me/2013/06/how-to-implement-file-upload-restful.html

1. I am wondering, if we don't use fiddler, how the client is going to send files because in fiddler I can see Upload files button.
2. or should I prefer making action which takes encoded contents of file as bytes?
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

If you have a REST API, then what does the web method take as an argument?  What does the "client" refer to?
Avatar of Dinesh Kumar

ASKER

1. so in other words, how to upload file to that REST API
2. Client can be .net client, php client, jquery
The following code I am trying, but it is saving some file which I can not read..

public async Task<HttpResponseMessage> Post()
        {
            // Check if the request contains multipart/form-data.
            if (!Request.Content.IsMimeMultipartContent())
            {
                throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
            }

            string root = HttpContext.Current.Server.MapPath("~/App_Data");
            var provider = new MultipartFormDataStreamProvider(root);

            try
            {
                // Read the form data.
                await Request.Content.ReadAsMultipartAsync(provider);

                // This illustrates how to get the file names.
                foreach (MultipartFileData file in provider.FileData)
                {
                    Trace.WriteLine(file.Headers.ContentDisposition.FileName);
                    Trace.WriteLine("Server file path: " + file.LocalFileName);
                }
                return Request.CreateResponse(HttpStatusCode.OK);
            }
            catch (System.Exception e)
            {
                return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e);
            }
        }

Open in new window

It looks like you are using the code from this article:

Sending HTML Form Data in ASP.NET Web API: File Upload and Multipart MIME
http://www.asp.net/web-api/overview/advanced/sending-html-form-data,-part-2

How are you sending the multi-part form data?
I am using fiddler to upload file.
I need to upload any file, let it be excel, text file or pdf.. by using oData service.
Just created another question :( with slight difference..
I don't think that the problem is with the web service.  I think that the problem is with the call from Fiddler.  What are you using for arguments?
I am using the same way as I tested following and that worked with mvc application.

http://blog.anthonybaker.me/2013/06/how-to-implement-file-upload-restful.html

method: post
url provided
and click on upload files and execute..
oh the problem is that the file was renamed while saving and there was no extention. the type of the file was file

so when I uploaded excel, I opened the saved file in excel, it opened.

so It seems the tasks remaining are to apply the extension and keep the same name as uploaded.
ASKER CERTIFIED SOLUTION
Avatar of Dinesh Kumar
Dinesh Kumar
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
finally its working..