简体   繁体   English

ASP.NET文件上传在Windows Azure中不起作用

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

I have write some codes for upload file in ASP.NET MVC3 project. 我在ASP.NET MVC3项目中为上传文件编写了一些代码。 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. 但是,当我在windowsazure.com上托管该网站时,一切正常,但文件上传。

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. 您可能希望将其包装为try / catch,以确保您的应用程序具有NTFS权限,以便在该位置编写和创建目录。 Also, ensure that your path variable is rendering out the path that you think it should be. 此外,确保您的path变量呈现出您认为应该是的路径。

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. 此外,如果VS项目中存在该目录,但其中没有任何内容,则编译器将不会创建该目录。 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. 要确保在上载项目时创建目录,请将空文本文件(例如“_doNotDelete.txt”)插入该目录,并将其构建操作设置为内容。 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. 首先,您不应该在临时操作旁边使用Web应用程序的文件夹。 Since Azure means multi-computer environment, resource (image) won't be available for requester if you use more than one instance (machine) 由于Azure意味着多计算机环境,如果您使用多个实例(机器),则资源(图像)将无法供请求者使用

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. 假设您在实例A的本地上传,实例B的本地将没有该文件,并且从DB检索路径不会更改实例B的任何内容。您永远不会知道哪个实例将响应请求。 (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. (好吧,你可以,但它超出了这里的范围)但最后你必须意识到你的上传请求可能会转到实例A而你的显示请求可能会转到无法检索的实例B.

Anyway, the best practice is use directly blobs, their whole purpose is this. 无论如何,最好的做法是直接使用blob,它们的全部目的就是这个。 You may find more details on http://www.windowsazure.com/en-us/documentation/articles/storage-dotnet-how-to-use-blobs-20/ 您可以在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/...") 但是如果你坚持使用本地路径并且没有丢失文件的问题(是的,它会发生)使用Server.MapPath("~/App_Data/...")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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