我一直在尝试上传文件到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