.NET Core----七牛云图片上传

时间:2024-08-16 21:33:20

在引用官方的qiniu.core的nuget是总是发生错误,错误如下,所以就在网上找到了一种解决方案,并稍微改进了一番,

以下是引用图:

.NET Core----七牛云图片上传

方法一:

第一步:引用nuge

.NET Core----七牛云图片上传

第二步:新建项目新建控制器视图、添加配置信息类和读取配置信息

配置信息类:

public class QnySetting
{
public string qiniuyunAK { get; set; }
public string qiniuyunSK { get; set; }
public string qiniuyunBucket { get; set; }
public string prefixPath { get; set; }
}  

配置信息:

 "Qny": {
"qiniuyunAK": "*",//ak
"qiniuyunSK": "*",//sk
"qiniuyunBucket": "**",//存储空间名称
"prefixPath": "http://upload.qiniup.com"//七牛云地址
}

  

读取配置信息(在startup文件中的ConfigureServices添加):

  services.Configure<QnySetting>(this.Configuration.GetSection("Qny"));

  

第三步:复制控制器及视图代码

视图代码:

 <form method="post" action="/Upload/UploadQiniu" enctype="multipart/form-data">
<input name="file" type="file" />
<input type="submit" value="提交" />
</form>

  控制器代码:

  private readonly QnySetting _Qny;
public UploadController(IOptions<QnySetting> Qny)
{
_Qny = Qny.Value;
} public IActionResult Index()
{
return View();
} [HttpPost]
public List<Object> UploadQiniu()
{
Mac mac = new Mac(_Qny.qiniuyunAK, _Qny.qiniuyunSK);// AK SK使用
PutPolicy putPolicy = new PutPolicy();
putPolicy.Scope = _Qny.qiniuyunBucket;
string token = Auth.CreateUploadToken(mac, putPolicy.ToJsonString());//token生成
IFormFileCollection files = Request.Form.Files;
Config config = new Config()
{
Zone = Zone.ZONE_CN_East,
UseHttps = true
};
var res = Request.Form.ToArray(); FormUploader upload = new FormUploader(config);
HttpResult result = new HttpResult();
List<Object> list = new List<Object>();
foreach (IFormFile file in files)//获取多个文件列表集合
{
if (file.Length > 0)
{
var _fileName = ContentDispositionHeaderValue
.Parse(file.ContentDisposition)
.FileName
.Trim('"');
var _qiniuName = _Qny.prefixPath + "/" + DateTime.Now.ToString("yyyyMMddHHmmssffffff") + _fileName;//重命名文件加上时间戳
Stream stream = file.OpenReadStream();
result = upload.UploadStream(stream, _qiniuName, token, null);
if (result.Code == 200)
{
list.Add(new { fileName = _fileName, qiniuName = _qiniuName, uploadTime = DateTime.Now});
}
else
{
throw new Exception(result.RefText);//上传失败错误信息
}
}
}
return list;
}

  第四步:然后运行上传图片,结果如图所示

.NET Core----七牛云图片上传

上文方法来自:https://www.cnblogs.com/OMango/p/8447480.html  其中优化了一下

方法二:

引用官方的Qiniu.Shared  nuget包

 public IActionResult UploadImgToQiNiu()
{
IFormFileCollection files = Request.Form.Files;
Mac mac = new Mac("你的ak", "你的sk");//这里的ak sk 也可以像上面一样通过配置实现
// 上传策略,参见
// https://developer.qiniu.com/kodo/manual/put-policy
PutPolicy putPolicy = new PutPolicy();
// 如果需要设置为"覆盖"上传(如果云端已有同名文件则覆盖),请使用 SCOPE = "BUCKET:KEY"
// putPolicy.Scope = bucket + ":" + saveKey;
putPolicy.Scope = "上传储存的空间名称";//可以像上文一样配置
// 上传策略有效期(对应于生成的凭证的有效期)
putPolicy.SetExpires();
// 上传到云端多少天后自动删除该文件,如果不设置(即保持默认默认)则不删除
// putPolicy.DeleteAfterDays = 1;
string jstr = putPolicy.ToJsonString();
//获取上传凭证
var uploadToken = Auth.CreateUploadToken(mac, jstr); //false 使用https 自动识别存储空间
Qiniu.Common.Config.AutoZone(_qiniu.qiniuyunAK, _qiniu.qiniuyunBucket, false);
            UploadManager um = new UploadManager();
foreach (IFormFile file in files)//获取多个文件列表集合
{
if (file.Length > )
{
Stream stream = file.OpenReadStream();
var fileName = ContentDispositionHeaderValue
.Parse(file.ContentDisposition)
.FileName
.Trim('"');
var saveKey = "http://upload.qiniup.com" + "/" + DateTime.Now.ToString("yyyyMMddHHmmssffffff") + fileName;//重命名文件加上时间戳 其中上传地址也可以配置
HttpResult result = um.UploadStream(stream, saveKey, uploadToken); if (result.Code == )
{
return Json(result.Text);
}
else
{
throw new Exception(result.RefText);//上传失败错误信息
}
}
}
return null;
}

如有问题,欢迎指正