标签:.com 优化 重命名 orm put 错误信息 string tar input
在引用官方的qiniu.core的nuget是总是发生错误,错误如下,所以就在网上找到了一种解决方案,并稍微改进了一番,
以下是引用图:
第一步:引用nuget(是位大神写的,官方的qiniu.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; }
第四部:然后运行上传图片,结果如图所示
本文来自:https://www.cnblogs.com/OMango/p/8447480.html 其中优化了一下
标签:.com 优化 重命名 orm put 错误信息 string tar input
原文地址:https://www.cnblogs.com/wuyabaibsd/p/9475180.html