简体   繁体   中英

Downloading file from WCF REST with Azure Blob Storage

I have a WCF REST service with Azure Blob Storage. I don't want to use my service as a proxy with a stream, so I wanted to redirect client's request to the absolute blob url but it redirects to the file and tries to open it directly. All I want is starting download process when server redirects the request to the absolute blob url. The code which I have tried is below:

    var sas = blob.GetSharedAccessSignature(new SharedAccessPolicy
    {
        Permissions = SharedAccessPermissions.Read,
        SharedAccessStartTime = DateTime.UtcNow,
        SharedAccessExpiryTime = DateTime.UtcNow + TimeSpan.FromMinutes(1)
    });
    WebOperationContext.Current.OutgoingResponse.ContentType = blob.Properties.ContentType;
    WebOperationContext.Current.OutgoingResponse.ContentLength = blob.Properties.Length;
    WebOperationContext.Current.OutgoingResponse.Location = blob.Uri.AbsoluteUri+sas;
    WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.Redirect;

PS I tried to change status code to Redirect, Moved.. etc. but nothing changes.

Update If I change ContentType to application/octet-stream, it results like this in IE:

<base64Binary xmlns="http://schemas.microsoft.com/2003/10/Serialization/">
...
</base64Binary>

Possible duplicate of this question .

Since Stive Marx sais that there is no way to control the "content-disposition" header for Azure Lbob, there is no way for force browser (IE) to download the file. Having the content-type set to "application/octet-stream" might force some browsers to downlaod the file, instead of opening it, but apparently it does not work for IE.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM