Solved

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

Posted on 2013-06-20
2
1,686 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 20

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 75

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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Excel Web Add-in Where is Visual Basic used 9 68
Import a excel sheet in a grid 2 42
asp.net mvc5 6 32
ASP.NET VB code for check and insert in a table 4 39
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

738 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