简体   繁体   English

C#文件上传在本地工作; 不在Azure上

[英]C# File Upload works locally; not on Azure

I have a C# web application which uploads an image file to Azure Blob Storage. 我有一个C#Web应用程序,它将图像文件上传到Azure Blob存储。 I am passing the local path of image file from a textbox (no File Upload Controller). 我正在从文本框(没有文件上传控制器)传递图像文件的本地路径。 This application works locally as expected. 此应用程序可以在本地正常工作。 But when I publish it on Azure, it throws exception. 但是,当我在Azure上发布它时,它会引发异常。

Could not find file (filename) 找不到文件(文件名)

What changes should be made to run it on Azure? 要在Azure上运行它应该进行哪些更改?

Code : 代码:

CloudBlobContainer container = Program.BlobUtilities.GetBlobClient.GetContainerReference(Container);// container 
            container.CreateIfNotExists();
            container.SetPermissions(new BlobContainerPermissions
            {
                PublicAccess = BlobContainerPublicAccessType.Blob
            });
            CloudBlobDirectory directory = container.GetDirectoryReference(foldername);
            // Get reference to blob (binary content)
            CloudBlockBlob blockBlob_image = directory.GetBlockBlobReference(imageid);
                 using (var filestream = System.IO.File.OpenRead(image_path))
                {
                    blockBlob_image.UploadFromStream(filestream);
                }

Could not find file (filename) 找不到文件(文件名)

The exception is caused by System.IO.File.OpenRead(filePath) , as you Web Application is published to Azure. Web应用程序发布到Azure时,此异常是由System.IO.File.OpenRead(filePath)引起的。 If you want use System.IO.File.OpenRead(filePath), you need to make sure that the filepath could be found in the WebApp . 如果要使用System.IO.File.OpenRead(filePath),则需要确保可以在WebApp中找到文件路径。

What changes should be made to run it on Azure? 要在Azure上运行它应该进行哪些更改?

If you want to use the code on the Azure, you need to make sure that the file could be found in the Azue Website. 如果要在Azure上使用代码,则需要确保可以在Azue网站中找到该文件。 You need to copy the file to Azure. 您需要将文件复制到Azure。 If you want to upload file to the Azure blob, it is not recommanded. 如果要将文件上传到Azure blob,则不建议使用。 Since that you need to copy the file to Azure WebApp first. 从那以后,您需要首先将文件复制到Azure WebApp。

Also As you mentioned that you could use FileUpload Controller to do that. 另外,正如您提到的,您可以使用FileUpload Controller来做到这一点。

You're probably using the path to your computer which will be different on azure. 您可能正在使用通往计算机的路径,这在天蓝色时会有所不同。 You can try change the path to something like this: 您可以尝试将路径更改为以下内容:

string path = HostingEnvironment.ApplicationPhysicalPath + @"\YourProjectName\PathToFile"

Ok, Found the solution. 确定,找到解决方案。 Instead of passing file path to a textbox, I used FileUpload Controller. 我没有使用文件路径到文本框,而是使用FileUpload Controller。 In the code-behind, 在后面的代码中

Stream image_path = FileUpload1.FileContent;

Actually, earlier too I had tried using FileUpload controller, but Server.MapPath(FileUpload1.Filename) and Path.GetFullPath(FileUpload1.FileName) were not giving the correct path. 实际上,我也曾尝试过使用FileUpload控制器,但是Server.MapPath(FileUpload1.Filename)Path.GetFullPath(FileUpload1.FileName)没有提供正确的路径。

Also, 也,

           using (var filestream = image_path)
           {
        blockBlob_image.UploadFromStream(image_path);
           }

is replaced by 被替换为

blockBlob_image.UploadFromStream(image_path);

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

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