圖1
圖2
圖3
圖4
1. 上傳百度云盤功能,由于百度開發者中還沒有開放對.net
操作的SDK,所以我們現在只能使用原生的REST API

我們的做法就是如何用C# 語言調用 調用curl 命令。
2. curl是利用URL語法在命令行方式下工作的開源文件傳輸工具。它被廣泛應用在Unix、多種linux發行版中,并且有DOS和Win32、Win64下的移植版本.
要操作curl 我們需要引入LibCurlNet.dll

3.百度上傳我們需要有百度賬號,而且需要申請開發者功能進入主頁后會有
accessKey,Secrectkey 這兩個Key非常重要
4. 傳入參數獲取加密url,下面一部分代碼是把文件寫入服務器Temp下的一個臨時文件。
public static string PUT(string sobject, HttpPostedFileBase file) { string content = Flag + "/n" + "Method=PUT/n" + "Bucket=" + Bucket + "/n" + "Object=" + sobject + "/n"; //+ "Time=" + "/n" //+ "ip=" + "/n" //+ "Size=" + "/n"; string signture = Flag + ":" + AccessKey + ":" + HttpUtility.UrlEncode(MyHmac.hmacSha1(content, SecrectKey)); string url = "http://bcs.duapp.com/" + Bucket + "/" + HttpUtility.UrlEncode(sobject) + "?sign=" + signture; string path = System.IO.Path.Combine(HttpContext.Current.Server.MapPath("~/Temp"), System.IO.Path.GetFileName(file.FileName)); Stream stream = file.InputStream; // 把 Stream 轉換成 byte[] byte[] bytes = new byte[stream.Length]; stream.Read(bytes, 0, bytes.Length); // 設置當前流的位置為流的開始 stream.Seek(0, SeekOrigin.Begin); FileStream fs = new FileStream(path, FileMode.Create); BinaryWriter bw = new BinaryWriter(fs); bw.Write(bytes); fs.Close(); FileStream fss = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read); try { Curl.GlobalInit((int)CURLinitFlag.CURL_GLOBAL_ALL); Easy easy = new Easy(); Easy.ReadFunction rf = new Easy.ReadFunction(MyHmac.OnReadData); easy.SetOpt(CURLoption.CURLOPT_READFUNCTION, rf); easy.SetOpt(CURLoption.CURLOPT_READDATA, fss); Easy.WriteFunction wf = new Easy.WriteFunction(MyHmac.OnWriteData); easy.SetOpt(CURLoption.CURLOPT_URL, url); easy.SetOpt(CURLoption.CURLOPT_UPLOAD, 1); easy.SetOpt(CURLoption.CURLOPT_INFILESIZE, bytes.LongLength); easy.SetOpt(CURLoption.CURLOPT_VERBOSE, 1); easy.SetOpt(CURLoption.CURLOPT_WRITEFUNCTION, wf); // easy.SetOpt(CURLoption.CURLOPT_WRITEDATA, wf); Easy.DebugFunction df = new Easy.DebugFunction(MyHmac.OnDebug); easy.SetOpt(CURLoption.CURLOPT_DEBUGFUNCTION, df); easy.SetOpt(CURLoption.CURLOPT_VERBOSE, true); CURLcode code = easy.Perform(); easy.Cleanup(); fss.Close(); Curl.GlobalCleanup(); //刪除臨時文件 File.Delete(path); return code.ToString(); } catch (Exception ex) { throw new Exception("Error", ex); } }
5. 這個代碼就是調用LibCurlNet上傳的代碼。
6. 這個是Action代碼調用百度上傳Put后在把這個文件名存儲到session中
public JsonResult UploadFile(HttpPostedFileBase[] fileDataList) { List<JsonModel> listInfo = new List<JsonModel>(); if (fileDataList != null) { try { foreach (HttpPostedFileBase file in fileDataList) { string fileExtension = Path.GetExtension(file.FileName); // 文件擴展名 string sobject = "/newFolder/" + Path.GetFileName(file.FileName); string path = System.IO.Path.Combine(Server.MapPath("~/Temp"), System.IO.Path.GetFileName(file.FileName)); file.SaveAs(path); string code = HttpClientUtil.doPutMethodToObj<string>(sobject, path); //string code = Crul.PUT(sobject, file); if (code == null) { viewModel = (MaterialsViewModel)Session["MaterialsViewModel"]; ObjectFiles of = new ObjectFiles(); of.ObjectType = "Material"; if (".bmp,.jpg,.tiff,.gif,.pcx,.tga,.exif,.fpx,.svg,.PSD,.cdr,.pcd,.dxf,.ufo,.eps,.ai,.raw".Contains(fileExtension)) { of.IsPic = true; } else { of.IsPic = false; } of.ObjectId = "M001";//Temp 值 of.FieldValue = sobject; int recNo = -1; if (viewModel.picturesList.Count > 0) { recNo = (viewModel.picturesList.Count + 1) * -1; } of.RecNo = recNo; viewModel.picturesList.Add(of); Session["MaterialsViewModel"] = viewModel; listInfo.Add(new JsonModel(true, file.FileName, "Uploaded successfully")); } else { listInfo.Add(new JsonModel(false, file.FileName, code)); } } return Json(listInfo, JsonRequestBehavior.AllowGet); } catch (Exception ex) { listInfo.Add(new JsonModel(false, "", ex.Message)); return Json(listInfo, JsonRequestBehavior.AllowGet); } } else
新聞熱點
疑難解答