Solved

ASP.NET WEB API File Upload (FileStream to FileUplaod)

Posted on 2013-06-20
2
1,579 Views
Last Modified: 2013-09-04
Hi!

I need to make a File Upload method in my ASP.NET Web API. The fileuploader shall be used by some native apps (iPhone / Android). I have never made this before, and don't know the best way to solve this.

When files are uploaded in my webproject I use a method that resizes and saves (uploads) the image. I would very much like to use this method in the WEB API also, but the problem is that this Method expects an FileUplad (not FileStrem) input. Is there any way to convert a FileStrem to a FileUpload?

Please see attached code, hope it illustrates what I try to achieve. If someone can help me fix this code I would be very thankful :)

public HttpResponseMessage PostProfileImage([FromUri]string filename)
{
            
    try
    {
        var task = this.Request.Content.ReadAsStreamAsync();
        task.Wait();
        Stream requestStream = task.Result;

            
        //USE "UploadAndResize" BELOW INSTEAD OF THIS:
        //Stream fileStream = File.Create(HttpContext.Current.Server.MapPath("~/" + filename));
        //requestStream.CopyTo(fileStream);
        //fileStream.Close();
        //requestStream.Close();


        // HERE I SHOULD MAKE A FILEUPLAOD OBJECT BASED ON THE FILESTREAM:
        System.Web.UI.WebControls.FileUpload myFile = new FileUpload();
                
        Int32 width = 500;
        bool resize = true;
        string imageName = myFile.FileName;
        string mapPath = ProfileImagePath(UserName.ToLower();

        // Save new image on disk (method expects FileUpload object)
        UploadAndResize uploader = new UploadAndResize();
        uploader.StartUploadProcess(myFile, width, resize, imageName, mapPath);

        Request.CreateResponse(HttpStatusCode.OK);
    }
    catch
    {
        Request.CreateResponse(HttpStatusCode.InternalServerError);
    }

}

Open in new window

0
Comment
Question by:webressurs
2 Comments
 
LVL 19

Expert Comment

by:Daniel Van Der Werken
ID: 39262288
I don't know. Seems like you are doing this in a way that's not the correct way to use the FileUpload class. I am looking at this:

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.fileupload.filecontent(v=vs.100).aspx

and have also written file upload functionality for aspx pages in the past. It's pretty straight forward. You use the control and the file stream comes in as part of the upload class itself.

I don't think you need to intercept the HTTP stream like you're doing here. Just implement the FileUpload control like it shows in the link I posted above.

Is there some reason you can't do that?
0
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 39263015
You shouldn't be attempting to use WebForms controls in MVC-style (including Web API) apps. Review the "Implement file uploading" section here for logic to perform file uploads in Web API:

http://blogs.msdn.com/b/codefx/archive/2012/02/23/more-about-rest-file-upload-download-service-with-asp-net-web-api-and-windows-phone-background-file-transfer.aspx
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now