1.服务端(主要用于生成上传,删除,下载等凭证)
引入Qiniu的Get包。包就叫Qiniu。
//上传凭证
//上传凭证:其余凭证类似的写法
public string UpLoadToken() { //密钥 Mac mac = new Mac("AK", "SK"); // 存储空间名 string Bucket = "wximagess"; //上传凭证 PutPolicy putPolicy = new PutPolicy(); //凭证有效时间 putPolicy.SetExpires(7200); //存储空间名 。覆盖上传要写成 putPolicy.Scope = Bucket:key; key是文件名 putPolicy.Scope = Bucket; //上传的内容定时删除(自定义)30天 putPolicy.DeleteAfterDays = 30; string token = Auth.CreateUploadToken(mac, putPolicy.ToJsonString()); return token; }
服务端直接下载
· public string Down() { Mac mac = new Mac("AK", "SK");
//下载的地址 string domain = "http://rkp2te65d.bkt.clouddn.com";
//下载的文件名 string key = "测试上传2";
//下载外链 3600外链有效期 string privateUrl = DownloadManager.CreatePrivateUrl(mac, domain, key, 3600); Console.WriteLine(privateUrl); return privateUrl; }
服务端直接上传
public string UpLoad() { //密钥 Mac mac = new Mac("AK", "SK"); //文件名 string key = "测试上传2"; // 本地文件路径 string filePath = "C:\\素材\\图片\\6a04b428gy1fyrlfhjaz1g207i07ijy0.gif"; // 存储空间名 string Bucket = "XXX"; //上传凭证 PutPolicy putPolicy = new PutPolicy(); putPolicy.SetExpires(7200); putPolicy.Scope = Bucket;
//自定义返回结果,可不设置,也可根据官网自定义,这里是官网给的自定义例子 putPolicy.ReturnBody = "{\"key\":\"$(key)\",\"hash\":\"$(etag)\",\"fsiz\":$(fsize),\"bucket\":\"$(bucket)\",\"name\":\"$(x:fname)\"}"; putPolicy.DeleteAfterDays = 30; //凭证 string token = Auth.CreateUploadToken(mac, putPolicy.ToJsonString()); Config config = new Config(); // 空间对应的机房.这里官网给的并不全,新出的空间地域无法直接引用,需要自己根据官网的地址New出新的机房、这里是华东浙江2 config.Zone = new Qiniu.Storage.Zone { ApiHost = "api.qiniuapi.com", CdnUpHosts = new string[1] { "upload-cn-east-2.qiniup.com" }, RsHost = "rs-cn-east-2.qiniuapi.com", RsfHost = "rsf-cn-east-2.qiniuapi.com", IovipHost = "iovip-cn-east-2.qiniuio.com", SrcUpHosts = new string[1] { "up-cn-east-2.qiniup.com" }, }; // 是否使用https域名 https收费,我不使用 config.UseHttps = false; // 上传是否使用cdn加速 config.UseCdnDomains = true; //表单上传。好像百兆以下都可以,超过百兆可以分片上传,图片使用表单上传就好,免费空间只有10G。上传图片还可,视频肯定不够用 FormUploader target = new FormUploader(config); HttpResult result = target.UploadFile(filePath, key, token, null); return "form upload result: " + result.ToString(); }
2.Ajax请求上传图片
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
function A() { $.ajax({ //请求服务端拿到上传凭证 url: "http://xxxx", type: "get", success: function (res) { var token = res; // localData是图片的base64数据,你可以用任何方式得到你图片的Base64编码 var localData = "XXXXX"; //七牛云只需要Base64后面的内容,因此把多余的前缀删除 localData = localData.replace("data:image/jgp;base64", ""); //开始上传 $.ajax({ url: "http://upload-cn-east-2.qiniup.com/putb64/-1", type: "post", beforeSend(request) { request.setRequestHeader("Content-Type", "application/octet-stream"); request.setRequestHeader("Authorization", "UpToken " + token); }, data: localData, success: function (res) { //上传成功后把文件名key存到自己的数据库里 var imageKey = res.key; $.ajax({ url: "http://XXXX/UploadImage", type: "post", data: imageKey, headers: { "Content-Type": "application/json" } , success: function (res) { alert(res); } }) } }) } }) }View Code
标签:putPolicy,七牛云,string,com,Ajax,API,key,new,上传 From: https://www.cnblogs.com/zhang-3/p/16853298.html