2019-11-04 oss實現瀏覽器強制下載

1. 需求瀏覽器

項目使用aliyun oss進行附件上傳管理,對於文本、圖片等上傳文件瀏覽器下載後會直接打開。ide

現須要下載至本地不直接打開。spa

 

2. 實現code

        /// <summary>
        /// 獲取下載連接 @hexx 2019-10-29
        /// </summary>
        /// <param name="bucketName"></param>
        /// <param name="key"></param>
        /// <param name="contentDisposition"></param>
        /// <param name="path"></param>
        /// <returns></returns>
        public static Uri GetPresignedUriByKey(string bucketName, string key, string contentDisposition)
        {
            try
            {
                var req = new GeneratePresignedUriRequest(bucketName, key, SignHttpMethod.Get);
                
                req.ResponseHeaders = new ResponseHeaderOverrides
                {
                    ContentDisposition = contentDisposition
                };

                var uri = client.GeneratePresignedUri(req);

                return uri;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

調用時將contentDisposition設爲"attachment"便可blog

using(AliyunOssHelper oss = new AliyunOssHelper())
{
    Uri uri = oss.GetUriFromOss(key, "attachment"); 
    ...    
}
相關文章
相關標籤/搜索