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?
Dinesh KumarAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bob LearnedCommented:
If you have a REST API, then what does the web method take as an argument?  What does the "client" refer to?
0
Dinesh KumarAuthor Commented:
1. so in other words, how to upload file to that REST API
2. Client can be .net client, php client, jquery
0
Dinesh KumarAuthor Commented:
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

0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

Bob LearnedCommented:
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?
0
Dinesh KumarAuthor Commented:
I am using fiddler to upload file.
0
Dinesh KumarAuthor Commented:
I need to upload any file, let it be excel, text file or pdf.. by using oData service.
0
Dinesh KumarAuthor Commented:
Just created another question :( with slight difference..
0
Bob LearnedCommented:
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?
0
Dinesh KumarAuthor Commented:
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..
0
Dinesh KumarAuthor Commented:
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.
0
Dinesh KumarAuthor Commented:
        /// <summary>
        /// Recieves file of any format and saves it to the server location
        /// </summary>
        /// <returns></returns>
        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 fileData in provider.FileData)
                {
                    string fileName = fileData.Headers.ContentDisposition.FileName;
                    fileName = CleanFileName(fileName);
                    File.Move(fileData.LocalFileName, Path.Combine(System.Configuration.ConfigurationManager.AppSettings["FileSavePath"], fileName));
                }
                return Request.CreateResponse(HttpStatusCode.OK);
            }
            catch (System.Exception e)
            {
                return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e);
            }
        }

        private static string CleanFileName(string fileName)
        {
            if (fileName.StartsWith("\"") && fileName.EndsWith("\""))
            {
                fileName = fileName.Trim('"');
            }
            if (fileName.Contains(@"/") || fileName.Contains(@"\"))
            {
                fileName = Path.GetFileName(fileName);
            }
            return fileName;
        } 

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Dinesh KumarAuthor Commented:
finally its working..
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.