Solved

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

Posted on 2013-06-20
2
1,666 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

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Open dialog with server side controls in it 3 46
Visual Studio hangs on running project 6 56
Aspx calendar pop up control 3 26
Return array 3 19
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

740 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