Asp.net uploads files to other URLs in the background by sending binary streams for storage. asp.net uploads files.
In fact, there are usually separate sites that store static files, such as slice and office documents. Website A needs to upload files to Site B for operations,
The following describes a method to use the UploadData method of the System. Net. WebClient class.
UploadFile. aspx html:
<form id="form1" runat="server"> <div> <asp:FileUpload runat="server" ID="fileUp" AllowMultiple="true" /> <asp:Button runat="server" ID="btnLoad" Text="Upload" OnClick="btnLoad_Click" /> </div> </form>
UploadFile. cs background code
Protected void btnLoad_Click (object sender, EventArgs e) {if (fileUp. hasFile) {var list = fileUp. postedFiles; foreach (var item in list) {string fileDoc = System. IO. path. getExtension (item. fileName ). toLower (); // suffix int fileLength = (int) item. inputStream. length; // InputStream. length; byte [] byteFile = new byte [fileLength]; item. inputStream. read (byteFile, 0, fileLength); item. inputStream. seek (0, System. IO. seekOrigin. begin); // set the current stream location after reading the stream, because md5 encryption also requires the stream string md5Value = Com. MD5.MyMD5. getMd5Hash (item. inputStream); // get the MD5string url of the uploaded file stream = string. format (" http://localhost:4884/SaveFile.ashx?name= "+ Md5Value +" & ext = "+ fileDoc); // path of the processing file for saving the stream: System. net. webClient wc = new System. net. webClient (); byte [] bts = wc. uploadData (url, "POST", byteFile); wc. dispose (); string filePath = System. text. encoding. UTF8.GetString (bts); // obtain SaveFile. ashx return }}}
For the Com. MD5.MyMD5. getMd5Hash method, see my C # MD5 Digest algorithm and hash algorithm.
SaveFile. ashx Handler
/// <Summary> /// summary of SaveFile /// </summary> public class SaveFile: IHttpHandler {public void ProcessRequest (HttpContext context) {lock (context. request. inputStream) {HttpRequest Request = context. request; string path = ""; string filenName = ""; string re = ""; if (Request. httpMethod. toLower () = "post") {try {using (Request. inputStream) {string name = Request ["name"]; string s = Request ["ext"]; byte [] bytes = new byte [Request. inputStream. length]; Request. inputStream. read (bytes, 0, bytes. length); Request. inputStream. seek (0, SeekOrigin. begin); // set the current stream position // Request. inputStream. position = 0; Request. inputStream. flush (); Request. inputStream. close (); Request. inputStream. dispose (); Random rnd = new Random (); int n = rnd. next (1000,999 9); // filenName = "/file/" + n. toString () + "-" + DateTime. now. toString ("yyyyMMddHHmmssss") + s; filenName = "/file/" + name + s; path = context. server. mapPath (filenName); FileStream fs = new FileStream (path, FileMode. create); BinaryWriter bw = new BinaryWriter (fs); bw. write (bytes); bw. close (); fs. close (); re = filenName; // System. threading. thread. sleep (500) ;}} catch (Exception ex) {}} context. response. contentType = "text/plain"; context. response. write (re); context. response. end () ;}} public bool IsReusable {get {return false ;}}}