我一直在尝试将文件上传到 Azure存储容器。 当我在本地运行我的网络应用程序时,如果网站 root 中的文件不,我会收到错误:
Could not find file 'C:\Program Files (x86)\IIS Express\test.txt'.
如果我将文件复制到网络应用根文件夹,则可以正常使用。
在云上运行网络应用我收到错误:
Could not find file 'D:\Windows\system32\test.txt'.
我无法从 HttpPostedFileBase 对象中获取完整本地文件路径。
代码:
private string UploadFile(HttpPostedFileBase file, string folder)
{
try
{
var date = DateTime.UtcNow.ToString("yyyyMMdd-hhmmss-");
var fileName = Path.GetFileName(file.FileName);
CloudStorageAccount storageAccount = CloudStorageAccount.Parse("DefaultEndpointsProtocol=http;AccountName=test;AccountKey=asdfasfasdasdf");
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference(folder);
bool b = container.CreateIfNotExists();
CloudBlockBlob blockBlob = container.GetBlockBlobReference(date + fileName);
blockBlob.Properties.ContentType = file.ContentType;
using (var fileStream = System.IO.File.OpenRead(fileName))
{
blockBlob.UploadFromStream(fileStream);
}
return blockBlob.Uri.AbsoluteUri;
}
catch (Exception ex)
{
return ex.Message;
}
答案 0 :(得分:3)
HttpPostedFileBase manual有这个说法;
FileName获取 客户端 上文件的完全限定名称。
也就是说,文件名不是可以在服务器上打开的文件名。
我认为你真正想做的是使用InputStream property;
blockBlob.Properties.ContentType = file.ContentType;
blockBlob.UploadFromStream(file.InputStream); // Upload from InputStream
return blockBlob.Uri.AbsoluteUri;
答案 1 :(得分:2)
非常感谢你:Joachim Isaksson(上图)昨天我花了整整一天的时间与这一行代码进行斗争。我已经对您的答案进行了投票,但我认为我现在要在我自己的代码中添加您的解决方案的副本:
On macOS systems, verify that you have added the following paths to the
PATH environment variable.
For Genymotion earlier than 2.6:
/Applications/Genymotion.app/Contents/MacOS/
/Applications/Genymotion Shell.app/Contents/MacOS/
For Genymotion 2.6:
/Applications/Genymotion.app/Contents/MacOS/player.app/Contents/MacOS
/Applications/Genymotion Shell.app/Contents/MacOS/
For example: Run the following command
export PATH=$PATH:/Applications/Genymotion\ Shell.app/Contents/MacOS/:/Applications/Genymotion.app/Contents/MacOS/