我一直在尝试将文件上传到Azure 存储容器。当我在本地运行我的网络应用程序时,如果文件不在网站根目录中,我会收到错误消息:
Could not find file 'C:\Program Files (x86)\IIS Express\test.txt'.
如果我将文件复制到web 应用程序根文件夹,它工作正常。
在云上运行Web 应用程序时出现错误:
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;
}