Solved

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

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Keyboard 2 33
Problem to refer to value 8 49
VB.Net Tranfer data between to VB.Net apps 8 25
Can I command line build a C# application? 6 33
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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

777 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