简体   繁体   中英

ASP.NET file upload doesn't work in windows azure

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.

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.

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/...")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM