The actual situation usually has a separate site for static files, tablets, Office documents, and so on. The operation of a site requires uploading files to site B,
Here's a way to pass the Uploaddata method of the System.Net.WebClient class.
Uploadfile.aspx HTML:
<formID= "Form1"runat= "Server"> <Div> <Asp:fileuploadrunat= "Server"ID= "Fileup"AllowMultiple= "true" /> <Asp:buttonrunat= "Server"ID= "BtnLoad"Text= "Upload"OnClick= "Btnload_click" /> </Div> </form>
UploadFile.cs Background Code
protected voidBtnload_click (Objectsender, EventArgs e) { if(fileup.hasfile) {varList =Fileup.postedfiles; foreach(varIteminchlist) { stringFiledoc = System.IO.Path.GetExtension (item. FileName). ToLower ();//suffix name intFilelength = (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); After reading the stream, set the location of the current stream, because MD5 encryption also needs to use the streamstringMd5value =Com.MD5.MyMD5.getMd5Hash (item. InputStream); Get the upload file stream MD5stringURL =string. Format ("http://localhost:4884/SaveFile.ashx?name="+ Md5value +"&ext="+Filedoc); Save the path to the processing file for the stream System.Net.WebClient WC=NewSystem.Net.WebClient (); byte[] BTS = WC. Uploaddata (URL,"POST", Bytefile); Wc. Dispose (); stringFilePath =System.Text.Encoding.UTF8.GetString (BTS);//Get savefile.ashx return}} }
Com.MD5.MyMD5.getMd5Hash method See: My C # MD5 digest algorithm, hash algorithm
SAVEFILE.ASHX processing Program
/// <summary> ///Summary description of SaveFile/// </summary> Public classSavefile:ihttphandler { Public voidProcessRequest (HttpContext context) {Lock(context. Request.inputstream) {HttpRequest Request=context. Request; stringPath ="";stringFilenname =""; stringRe =""; if(Request.HttpMethod.ToLower () = ="Post") { Try { using(request.inputstream) {stringName = request["name"]; strings = request["ext"]; byte[] bytes =New byte[Request.InputStream.Length]; Request.InputStream.Read (Bytes,0, bytes. Length); Request.InputStream.Seek (0, Seekorigin.begin); // Set the position of the current stream //Request.InputStream.Position = 0;Request.InputStream.Flush (); Request.InputStream.Close (); Request.InputStream.Dispose (); Random Rnd=NewRandom (); intn = rnd. Next ( +,9999); //filenname = "/file/" + n.tostring () + "-" + DateTime.Now.ToString ("yyyymmddhhmmssss") + S;Filenname ="/file/"+name +s; Path=context. Server.MapPath (Filenname); FileStream FS=NewFileStream (path, filemode.create); BinaryWriter BW=NewBinaryWriter (FS); Bw. Write (bytes); Bw. Close (); Fs. Close (); Re=Filenname;//system.threading.thread.sleep ( -); } } Catch(Exception ex) {}} context. Response.ContentType="Text/plain"; Context. Response.Write (re); Context. Response.End (); } } Public BOOLisreusable {Get { return false; } } }
ASP. NET upload file background sent to other URLs via binary stream save