七牛云文件上传

时间:2021-11-05 04:48:31

七牛云:错误"{\"error\":\"incorrect region, please use up-z2.qiniu.com\"}"

解决方法:添加Qiniu.Common.Config.AutoZone(access_key, bucket, true);

 

文件上传:
 1 //文件上传
2 public ActionResult fileUpload(string type, string filename, string midiname, string keyValue)
3 {
4   HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;
5   //没有文件上传,直接返回
6   if (files[0].ContentLength == 0 || string.IsNullOrEmpty(files[0].FileName))
7   {
8     return HttpNotFound();
9 }
10 string virtualPath = "";
11 var newfilename = "";
12 string FileEextension = Path.GetExtension(files[0].FileName);
13 string[] name = midiname.Split('/');
14 string[] miname = name[name.Length - 1].Split('.');
15 if (!string.IsNullOrEmpty(midiname))
16 {
17 virtualPath = string.Format("/Resource/UploadFile/NetworkDisk/{0}{1}", miname[0], FileEextension);
18 }
19 else
20 {
21 virtualPath = string.Format("/Resource/UploadFile/NetworkDisk/{0}{1}", keyValue + type, FileEextension);
22 }
23   string fullFileName = Server.MapPath("~" + virtualPath);
24   //创建文件夹,保存文件
25   string path = Path.GetDirectoryName(fullFileName);
26   Directory.CreateDirectory(path);
27   files[0].SaveAs(fullFileName);
28   newfilename = string.Format("{0}{1}{2}{3}{4}{5}{6}{7}{8}{9}", name[0], "/", name[1], "/", name[2], "/",type,"/", miname[0], FileEextension);
29   var result = UploadFile(newfilename, fullFileName, keyValue, FileEextension);
30   if (result == "success")
31   {
32     return Success("操作成功。");
33   }
34   else {
35     return Error("操作失败!");
36   }
37 }

 

UploadFile:
 1 public string UploadFile(string saveKey, string localFile, string keyValue, string FileEextension)
2 {
3   string bucket = ConfigurationManager.AppSettings["bucket"].ToString();
4   string access_key = ConfigurationManager.AppSettings["access_key"].ToString();
5   string secret_key = ConfigurationManager.AppSettings["secret_key"].ToString();
6   // 生成(上传)凭证时需要使用此Mac
7   // 这个示例单独使用了一个Settings类,其中包含AccessKey和SecretKey
8   // 实际应用中,请自行设置您的AccessKey和SecretKey
9   Mac mac = new Mac(access_key, secret_key);
10   //string saveKey = "1.png";
11   //string localFile = "D:\\QFL\\1.png";
12   // 上传策略,参见
13   // https://developer.qiniu.com/kodo/manual/put-policy
14   PutPolicy putPolicy = new PutPolicy();
15   // 如果需要设置为"覆盖"上传(如果云端已有同名文件则覆盖),请使用 SCOPE = "BUCKET:KEY"
16   // putPolicy.Scope = bucket + ":" + saveKey;
17   putPolicy.Scope = bucket + ":" + saveKey;
18   // 上传策略有效期(对应于生成的凭证的有效期)
19   putPolicy.SetExpires(3600);
20   // 上传到云端多少天后自动删除该文件,如果不设置(即保持默认默认)则不删除
21   putPolicy.DeleteAfterDays = 1;
22   // 生成上传凭证,参见
23   // https://developer.qiniu.com/kodo/manual/upload-token
24   string jstr = putPolicy.ToJsonString();
25   string token = Auth.CreateUploadToken(mac, jstr);
26   Qiniu.Common.Config.AutoZone(access_key, bucket, true);
27   UploadManager um = new UploadManager();
28   HttpResult result = um.UploadFile(localFile, saveKey, token);
29   JObject jo = (JObject)JsonConvert.DeserializeObject(result.Text);
30   var hash = jo["hash"].ToString();
31   var key = jo["key"].ToString();
32 }

 

 

js:

 1 <script src="~/Content/scripts/plugins/uploadify/ajaxfileupload.js"></script>
2
3   //pdfupload
4 function pdfFileUpload() {
5   document.getElementById("pdffile").click();
6   var id = "pdffile";
7   var midiname = $("#midiname").val();
8   $("#pdffile").on('change', function (e) {
9   var name = e.currentTarget.files[0].name;
10   var FileEextension = name.substring(name.lastIndexOf(".")).toLowerCase();//获得文件后缀名
11   if (FileEextension != ".pdf") {
12     dialogMsg('请上传后缀名为.pdf的文件', 0);
13     return false;
14   }
15   $.ajaxFileUpload({
16     url: "../../BaseManage/t_musicinfo_ch/fileUpload?type=pdf" + "&filename=" + name + "&midiname=" + midiname + "&keyValue=" + keyValue,
17     secureuri: false,
18     fileElementId: id,
19     dataType: 'json',
20     success: function (data) {
21     dialogMsg(data.message, data.type);
22     initControl();
23   }
24 });
25 });
26 }

 

html:

1 <input type="file" id="pdffile" name="pdffile" style="display:none" accept=".pdf">
2 <button id="pdfupload"style="float:left;margin:0 5px;" type="button" class="btn btn-primary" onclick="pdfFileUpload()">pdf上传</button>