阿里云oss通过api上传图片后不能预览只能下载的解决方法

1、需要有自我域名支持。

2、ContentType要设置好。

拿图片来举例:上传base64

     public bool PutObject(string bucketName, string objectName, string ImageUrl)
        {         
            try
            {
                var objectmetadata = new ObjectMetadata();
                objectmetadata.ContentType = "image/jpg"; //==========================》》》》》》》》》》》》》》》要设置为这个
                objectmetadata.AddHeader("Pragma", "no-cache");
                byte[] imageByte = Convert.FromBase64String(ImageUrl);
                MemoryStream ms = new MemoryStream(imageByte);
                client.PutObject("smallshoposs", objectName, ms, objectmetadata);             
                return true;
            }
            catch (Exception)
            {              
                return false;
            }

        }

上传本地文件:

      public bool PutObjectLocal(string bucketName, string objectName, string localFilename)
        {
            try
            {
                var objectmetadata = new ObjectMetadata();
                objectmetadata.ContentType = "image/jpg";
                objectmetadata.AddHeader("Pragma", "no-cache");
                client.PutObject(bucketName, objectName, localFilename, objectmetadata);
                Console.WriteLine("Put object succeeded");
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine("Put object failed, {0}", ex.Message);
                return false;
            }
        }

 

posted @ 2021-03-04 16:50  幸福安康  阅读(2754)  评论(0编辑  收藏  举报