File Upload to Baidu cloud disk instructions, file upload to cloud Disk
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; // converts Stream to byte [] byte [] bytes = new byte [stream. length]; stream. read (bytes, 0, bytes. length); // set the current stream position to the starting stream of the 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 (); // delete a temporary File. delete (path); return code. toString () ;}catch (Exception ex) {throw new Exception ("Error", ex );}}
5. This Code is the code that calls LibCurlNet to upload.
6. The Action code calls Baidu to upload Put and stores the file name in the 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); // file extension 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 ();. objectType = "Material"; if (".bmp, .jpg,.htm, .gif ,. pcx ,. tga ,. exif ,. fpx ,. svg ,. psd ,. cdr ,. pcd ,. dxf ,. ufo ,. eps ,. ai ,. raw ". contains (fileExtension) {. isPic = true;} else {. isPic = false;}. objectId = "M001"; // Temp value. fieldValue = sobject; int recNo =-1; if (viewModel. picturesList. count> 0) {recNo = (viewModel. picturesList. count + 1) *-1;}. 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 {listInfo. add (new JsonModel (false, "", "Please select a file upload"); return Json (listInfo, JsonRequestBehavior. allowGet );}}
6. In Figure 4, the Delete function can only be deleted from the database, but not from the Baidu cloud disk,
I still don't know how to implement the Code for deleting Baidu cloud. There is too little information in China.
//public static bool DELETE(string sobject) //{ // string content = Flag + "\n" // + "Method=DELETE\n" // + "Bucket=" + Bucket + "\n" // + "Object=" + sobject + "\n"; // string signture = Flag + ":" + AccessKey + ":" + HttpUtility.UrlEncode(MyHmac.hmacSha1(content, SecrectKey)); // string url = "http://bcs.duapp.com/" + Bucket + "/" + HttpUtility.UrlEncode(sobject) + "?sign=" + signture; // return HttpClientUtil.doDeleteMethod(url); //}
Upload method to Baidu cloud No. 2
1. We mentioned above that calling the Baidu cloud interface requires adding libCurlNet. dll. LibCurlNet. dll is not required here.
Public static string GenerateUri (string method, string sobject) {string content = Flag + "\ n" + "Method =" + method + "\ n" + "Bucket =" + Bucket + "\ n" + "Object =" + sobject + "\ n "; // + "Time =" + "\ n" // + "Ip =" + "\ n" // + "Size =" + "\ n "; string signture = Flag + ":" + AccessKey + ":" + HttpUtility. urlEncode (MyHmac. hmacSha1 (content, SecrectKey); string uri = "http://bcs.duapp.com/" + Bucket + "/" + HttpUtility. UrlEncode (sobject) + "? Sign = "+ signture; return uri ;}
// REST @ PUT method, with the public static T doPutMethodToObj (string _ Object, string path) {string uri = GenerateUri ("PUT", _ Object ); httpWebRequest WRequest = (HttpWebRequest) WebRequest. create (uri); WRequest. method = "PUT"; WRequest. contentType = "application/json; charset = UTF-8"; WRequest. allowWriteStreamBuffering = false; // WRequest. timeout = 10000; FileStream ReadIn = new FileStream (path, FileMode. open, FileAccess. read); ReadIn. seek (0, SeekOrigin. begin); // Move to the start of the file. WRequest. contentLength = ReadIn. length; // Set the content length header to the size of the file. byte [] FileData = new Byte [ReadIn. length]; // Read the file in 2 KB segments. int DataRead = 0; Stream tempStream = WRequest. getRequestStream (); do {if (FileData. length <= 2048) DataRead = ReadIn. read (FileData, 0, FileData. length); else DataRead = ReadIn. read (FileData, 0, 2048); if (DataRead> 0) // we have data {tempStream. write (FileData, 0, DataRead); // Array. clear (FileData, 0, FileData. length); // Clear the array .}} while (DataRead> 0); HttpWebResponse WResponse = (HttpWebResponse) WRequest. getResponse (); // Read your response data here. // Close all streams. readIn. close (); tempStream. close (); string json = getResponseString (WResponse); WResponse. close (); // Delete the temporary File. delete (path); return JsonConvert. deserializeObject <T> (json );}
2. Delete Baidu cloud Disk Files
// REST @ DELETE method public static string doDeleteMethod (string _ Object) {string uri = GenerateUri ("DELETE", _ Object); HttpWebRequest request = (HttpWebRequest) WebRequest. create (uri); request. method = "DELETE"; request. contentType = "application/json; charset = UTF-8"; request. userAgent = "curl/7.19.7 (universal-apple-darwin10.0) libcurl/7.19.7 OpenSSL/0.9.8l zlib/1.2.3"; request. accept = "*/*"; request. contentLength = 0; using (HttpWebResponse response = (HttpWebResponse) request. getResponse () {using (StreamReader reader = new StreamReader (response. getResponseStream (), System. text. encoding. getEncoding ("UTF-8") {return reader. readToEnd ();}}}
3. Download Baidu cloud Disk Files
public static string GET(string sobject) { string uri = GenerateUri("GET", sobject); string _private = "&response-cache-control=private"; return uri + _private; }
How can I upload files to Baidu cloud disk to allow others to download my files?
Create a share, and then send the share link to someone else. When someone else opens the link, it can be directly transferred to its own network disk.
Sharing can be divided into public sharing and private sharing. It is best to share private files. Private sharing has an extraction code. You need to tell others together with the extraction code before others can open the link.
The landlord will understand it by experimenting with it.
How do I share files uploaded by Baidu cloud disk ??
How to share files uploaded by Baidu cloud Disk
Jingyan.baidu.com/...3.html
Baidu cloud disk is very convenient for uploading and downloading things. You can directly upload things and share them with your friends or on the Internet. Now we will teach you how to share the files uploaded by Baidu cloud disk.