我一直在嘗試上傳文件到Azure存儲容器。 當我在本地運行我的Web應用程序,如果文件是不在網站根我得到一個錯誤:windows 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;
}
感謝Joachim。它解決了這個問題。 – user2178726 2013-03-17 23:56:15