0

I've been trying to save incoming uploaded images in ~/Content on my development machine while debugging/testing, but no files are being transferred there. Does the folder allow for images to be saved/transferred to it, or will I need to save them in ~/App_Data?


EDIT: Code I'm using:

public ActionResult(AdminGameEditModel formData)
{
    Game game = new Game();

    AutoMapper.Mapper.Map<AdminGameEditModel, Game>(formData, game);

    if (formData.BoxArt.ContentLength > 0 && formData.IndexImage.ContentLength > 0)
    {
        var BoxArtName = Path.GetFileName(formData.BoxArt.FileName);
        var BoxArtPath = Path.Combine(Server.MapPath("~/Content/Images/BoxArt"), BoxArtName);
        game.BoxArtPath = BoxArtPath;
        formData.BoxArt.SaveAs(BoxArtPath);

        var IndexImageName = Path.GetFileName(formData.IndexImage.FileName);
        var IndexImagePath = Path.Combine(Server.MapPath("~/Content/Images/GameIndexImages"), IndexImageName);
        game.IndexImagePath = IndexImagePath;
        formData.IndexImage.SaveAs(IndexImagePath);
    }

    // rest of controller method
}

Both files are HttpPostedFileBase objects. The server I'm using currently is just the ASP.NET server that runs during VS debugging.

Major Productions
  • 5,914
  • 13
  • 70
  • 149

2 Answers2

2

You can copy/save images and files wherever you want on your disk.
You only have to check if the IIS process has access to the folder.
I tend to create a custom folder where I put my uploaded files.

LeftyX
  • 35,328
  • 21
  • 132
  • 193
1

You should be able to save uploaded files to any directory you choose. The catch is ensuring that the service account that ASP.NET runs under has write permissions to that folder. For IIS 7 or later, it is likely the Network Service account on the server. To be sure, look at the Application Pool your site is running under in IIS and check the identity it is running under.


Update:

I see. You can try this to see if there is an issue with the SaveAs method of that type (I remember some people having issues with it).

Instead of:

formData.BoxArt.SaveAs(BoxArtPath);

try:

using (var output = new FileStream(BoxArtPath, FileMode.CreateNew)) {
  var data = new byte[formData.BoxArt.ContentLength];
  formData.BoxArt.InputStream.Read(data, 0, data.Length);
  output.Write(data, 0, data.Length);
}
ventaur
  • 1,821
  • 11
  • 24
  • What if I'm not running on IIS at the moment? Testing is being done using the VS 2010 debugger. The site isn't actually deployed yet. – Major Productions May 26 '11 at 14:34
  • What code are you using to get the uploaded file and save it to that folder? – ventaur May 26 '11 at 14:35
  • 1
    Try changing these lines. var BoxArtName = Path.GetFileName(formData.BoxArt); to var BoxArtName = Path.GetFileName(formData.BoxArt.FileName); – ventaur May 26 '11 at 14:50
  • HttpPostedFileBase objects have a FileName property with their file name. I'm surprised Path.GetFileName used on type HttpPostedFileBase didn't bomb with an exception. – ventaur May 26 '11 at 14:51
  • I actually have that, too. Sorry, away from dev machine and was trying to write from memory. My code is based on what I read from a tutorial by Phil Haack: http://haacked.com/archive/2010/07/16/uploading-files-with-aspnetmvc.aspx – Major Productions May 26 '11 at 15:06
  • The path is generated properly, and no exceptions are thrown. I just don't have the images in the folders I specified after I step through the method – Major Productions May 26 '11 at 15:08
  • Great, thanks. :) I won't be able to try it until later tonight (USA, Eastern time), but I'll let you know my progress. – Major Productions May 26 '11 at 16:29
  • Gah, I'm an idiot. For some reason, the images weren't being listed in my Solution Navigator extension. Clicking 'Show All Files' shows them. Sorry for wasting your time, and thanks so much for trying to help. – Major Productions May 27 '11 at 00:07