I am uploading files to Azure blob storage using this code, where container
is my CloudBlobContainer
我正在使用此代码将文件上载到Azure blob存储,其中容器是我的CloudBlobContainer
public void SaveFile(string blobPath, Stream stream)
{
stream.Seek(0, SeekOrigin.Begin);
CloudBlockBlob blockBlob = container.GetBlockBlobReference(virtualPath);
blockBlob.Properties.ContentDisposition =
"attachment; filename=" + Path.GetFileName(virtualPath);
blockBlob.UploadFromStream(stream);
}
Then when a user clicks on a file in my web page I am trying to trigger a download where they are prompted to either save/open the file. I do this by calling an Action which returns a redirect to the blob URL.
然后,当用户单击我的web页面中的一个文件时,我正在尝试触发一个下载,提示他们保存/打开该文件。我通过调用一个返回到blob URL的重定向的操作来实现这一点。
public ActionResult LoadFile(string path)
{
string url = StorageManager.GetBlobUrlFromName(path);
return Redirect(url);
}
The issue is this will open the files in the browser e.g. the user will be redirect away from my site and shown a .jpg file in the browser when I was expecting them to stay on my page but start downloading the file.
问题是这将打开浏览器中的文件,例如,当我期望用户在我的页面上停留但开始下载文件时,用户将被重定向离开我的站点并在浏览器中显示一个.jpg文件。
2 个解决方案
#1
1
What you possibly miss is invoking blockBlob.SetProperties()
after setting properties.
您可能会错过在设置属性之后调用blockBlob.SetProperties()。
On my code it looks like this:
在我的代码中,它是这样的:
blob.CreateOrReplace();
blob.Properties.ContentType = "text/plain";
blob.Properties.ContentDisposition = "attachment; filename=" + Path.GetFileName(blobName);
blob.SetProperties(); // !!!
#2
0
One way to achieve what you want is for an MVC action to fetch the image from blob storage and return the File, ie:
实现您所需的一种方法是MVC操作从blob存储中获取图像并返回文件,即:
public ActionResult LoadFile(string path)
{
byteArray imageBytes = ....get img from blob storage
return File(byteArray, "image/png", "filename.ext");
}
#1
1
What you possibly miss is invoking blockBlob.SetProperties()
after setting properties.
您可能会错过在设置属性之后调用blockBlob.SetProperties()。
On my code it looks like this:
在我的代码中,它是这样的:
blob.CreateOrReplace();
blob.Properties.ContentType = "text/plain";
blob.Properties.ContentDisposition = "attachment; filename=" + Path.GetFileName(blobName);
blob.SetProperties(); // !!!
#2
0
One way to achieve what you want is for an MVC action to fetch the image from blob storage and return the File, ie:
实现您所需的一种方法是MVC操作从blob存储中获取图像并返回文件,即:
public ActionResult LoadFile(string path)
{
byteArray imageBytes = ....get img from blob storage
return File(byteArray, "image/png", "filename.ext");
}