![](/img/trans.png)
[英]Azure blob file got corrupted post uploading file using UploadFromStreamAsync
[英]Uploading a file to Azure using UploadFromFile
我有一个ASP.NET Web窗体应用程序。
在我的一种形式中,我正在从Azure下载PDF,并使用rasteredge(PDF查看器)显示PDF,这使我可以在PDF上添加和保存注释。
然后将带有批注的文件保存在应用程序根目录下的文件夹(RasterEdge_Cache)中。 我想使用UploadFromFile函数将PDF上传回Azure。
这是我正在使用的功能:
public static void UploadFile(DTO.BlobUpload b)
{
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["SN_ZEUXYS"]);
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference(b.Container);
CloudBlockBlob blockBlob = container.GetBlockBlobReference(b.FileName);
// b.FilePath = "~/RasterEdge_Cache/193304798925/output/A-0002-00008-00205Anno.pdf"
blockBlob.UploadFromFile(b.FilePath);
}
这是我收到的错误消息:
mscorlib.dll中发生类型'System.IO.DirectoryNotFoundException'的异常,但未在用户代码中处理。其他信息:找不到路径'C:\\ Program Files(x86)\\ IIS Express \\〜\\ RasterEdge_Cache的一部分\\ 193304798925 \\输出\\ A-0002-00008-00205Anno.pdf”。
我认为文件路径不正确,应该使用哪个路径,还是正确使用UploadFromFile?
谢谢您的帮助。
您的文件路径是相对路径,因此您应指定确切的位置。 考虑使用Server.MapPath方法来查找其到服务器上应用程序根目录的物理路径。
HttpContext httpContext = HttpContext.Current;
HttpServerUtility server = httpContext.Server;
b.FilePath = server.MapPath("~/RasterEdge_Cache/193304798925/X.pdf")
Microsoft.WindowsAzure.Storage.dll中发生类型'Microsoft.WindowsAzure.Storage.StorageException'的异常,但未在用户代码中处理。其他信息:远程服务器返回错误:(404)Not Found。 所以问题是,我是否使用了正确的函数:UploadFromFile? blockBlob.UploadFromFile(b.FilePath);
根据本文 ,“远程服务器返回错误:(404)未找到。” 当由于找不到容器或Blob而对容器的上载操作失败时,会发生这种情况。
因此,我建议您首先检查“ b.Container,b.FileName”的值是否存在,或者可以使用CreateIfNotExists方法。
更多细节,您可以参考以下代码,希望它给您一些提示。
protected void Button5_Click(object sender, EventArgs e)
{
CloudStorageAccount storageAccount = CloudStorageAccount.Parse("connection string");
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
//check the b.Container value is right and exists
CloudBlobContainer container = blobClient.GetContainerReference("foobar");
CloudBlockBlob blockBlob = container.GetBlockBlobReference("TestFile.pdf");
HttpContext httpContext = HttpContext.Current;
HttpServerUtility server = httpContext.Server;
string FilePath = server.MapPath("~/test/TestFile.pdf");
//by using this code will create the container if not exists
container.CreateIfNotExists();
blockBlob.UploadFromFile(FilePath);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.