3

I have write some codes for upload file in ASP.NET MVC3 project. In stead of storing file in database, I have uploaded the files in file system and stored the paths in database.

The codes for upload is as follows:-

if (file != null && file.ContentLength > 0)
{
    if (path == null)
    {
        throw new ArgumentNullException("path cannot be null");
    }
    string pFileName = PrefixFName(file.FileName);
    String relpath = String.Format("{0}/{1}", path, pFileName);
    try
    {
        file.SaveAs(Server.MapPath(relpath));
        return pFileName;
    }
    catch (HttpException e)
    {
        throw new ApplicationException("Cannot save uploaded file", e);
    }
}

After saving the file I have used that image with image tag in several views. My codes works fine in local. But when I have hosted the site in windowsazure.com all things are working but the file upload.

How can I get rid of this situation? Please help.

10
  • Is your application running as Website or Cloud Service? Also in how many instances your application is running? Commented Feb 8, 2014 at 14:34
  • What is not working? The file isn't saved, you can't access it, it throws an exception ... ? Commented Feb 8, 2014 at 14:37
  • @ Gaurav Mantri, my site is running as website and only one instance is running. FYI I have saved all the images in "~/Content/Images/MyFolder" Commented Feb 8, 2014 at 14:40
  • @Kenneth, Even I don't know what's the problem. It doesn't throw any exception. When I upload a file nothing happens. Commented Feb 8, 2014 at 14:43
  • Where do you set the "path" variable? Does that directory exist on your azure website? Commented Feb 8, 2014 at 16:31

2 Answers 2

7

One of the things you need to be aware of before trying to save the file is to ensure that the directory that you are wanting to save the file in exists. Here is a code snippet to ensure the target directory has been created on the target server.

   var path= Server.MapPath("~/ImagesDirectory");

   if (!System.IO.Directory.Exists(path))
   {
       System.IO.Directory.CreateDirectory(path);
   }

You may want to wrap this is a try/catch to ensure your application has the NTFS privileges to write and create directories in that location. Also, ensure that your path variable is rendering out the path that you think it should be.

Also, if the directory exists in your VS project, but does not have any content in it, then the compiler will not create that directory. To ensure the directory is created when you upload a project, insert an empty text file, such as "_doNotDelete.txt" into that directory and set it's build action to content. This will ensure the directory will be created when you do a publish.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the hint about the directories not being created if nothing inside them.
3

First you should not use web application's folder beside temporary operations. Since Azure means multi-computer environment, resource (image) won't be available for requester if you use more than one instance (machine)

Let's say you uploaded on instance A's local, instance B's local won't have that file and retrieving path from DB won't change anything for instance B. You would never know which instance will give the response to request. (well, you can but it is out of the scope here) But at the end you have to realize that your upload request may go to instance A and your display request may go to instance B which will not be able to retrieve.

Anyway, the best practice is use directly blobs, their whole purpose is this. You may find more details on http://www.windowsazure.com/en-us/documentation/articles/storage-dotnet-how-to-use-blobs-20/

But if you insist on using local path and have no problem losing files (yes it will happen) use Server.MapPath("~/App_Data/...")

2 Comments

Just as a note, this is most definitely true for azure web roles, but not web sites. Web sites uses some magic (and azure drive?) to allow "local" file storage which is distributed. It is essentially a blob based storage container for azure websites. MS goal with websites was to make it quick and easy to deploy a website (custom, wordpress, joombla, DotNetNuke) without having to worry about rewriting the entire project to deal with blob storage. This azure drive is available to all instances running on the azure websites. channel9.msdn.com/Events/TechEd/Europe/2012/AZR305
Thank you @Tommy I will watch that. Azure Drive is very old concept (it is there since beginning) but I didn't know that they have a new implementation for WebSites. Will check that. Thank you again

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.