Solved

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

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

717 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