This film mainly solve "type=file is feasible, type=image when I do not know why always prompt" system busy"" problem.
Nail Upload Material API documentation is relatively simple to write, especially the HTTP header that block completely did not write, demo also does not have. NET, cup with AH.
Read a man's post, the principle of writing is very clear, we can refer to.
http://blog.csdn.net/xxdddail/article/details/51983487
But there is a problem is not solved, the article also mentions, is type=file when is feasible, type=image when do not know why always prompt "system busy"
Studied for several days, referring to the following man's code
Https://github.com/icodegame/DTSigninRemind
Finally found the problem, it seems that C # provides the WebClient itself, instead of HttpWebRequest solved the problem.
In the case of custom headers WebClient not, but HttpWebRequest can.
If you want to know the HTTP header, please read this article, http://blog.csdn.net/xiaojianpitt/article/details/6856536
The above GitHub code is asynchronous, I need to synchronize the way, directly to the synchronization. If you need asynchronous code, you can use the code in the link above.
The code is as follows
1 Public Static stringPostfile (stringUrlstringFilewithpath)2 {3 varresult =string. Empty;4 varRequest =(HttpWebRequest) webrequest.create (URL);5 varBoundary ="----------"+ DateTime.Now.Ticks.ToString ("x");6Request. ContentType ="multipart/form-data; boundary="+boundary;7Request. Method ="POST";8 using(Stream Requeststream =request. GetRequestStream ())9 {Ten byte[] boundarybytes = Encoding.UTF8.GetBytes ("--"+ Boundary +"\ r \ n"); One byte[] trailer = Encoding.UTF8.GetBytes ("\r\n--"+ Boundary +"–-\r\n"); A varfilename =Path.getfilename (filewithpath); - using(FileStream fs =NewFileStream (Filewithpath, FileMode.Open, FileAccess.Read)) - { the byte[] BArr =New byte[FS. Length]; -Fs. Read (BARR,0, barr.length); -requestStream.Write (Boundarybytes,0, Boundarybytes. Length); - varHeader = $"content-disposition:form-data;name=\ "Media\"; filename=\ "{filename}\" \r\nfilelength=\ "{fs. Length}\ "\r\ncontent-type:application/octet-stream\r\n\r\n"; + byte[] Postheaderbytes =Encoding.UTF8.GetBytes (header. ToString ()); -requestStream.Write (Postheaderbytes,0, postheaderbytes.length); + FS. Close (); ArequestStream.Write (BARR,0, barr.length); atrequestStream.Write (Trailer,0, Trailer. Length); - } - } - varResponse =(HttpWebResponse) request. GetResponse (); - varResponsestream =Response. GetResponseStream (); - using(varStreamReader =NewStreamReader (response. GetResponseStream ())) in { -result =streamreader.readtoend (); to } + returnresult; -}
Calling code
1 var @" d:\a.jpg " ; 2 var url = $"https://oapi.dingtalk.com/media/upload?access_token={access_token}&type= Image"; 3 var requestresult = postfile (URL, fileName);
The returned result
. NET C # write spike upload material interface to solve busy return system problems