Asp.net上傳檔案後台通過二進位流發送到其他Url儲存

來源:互聯網
上載者:User

標籤:

實際情況一般有單獨的網站存放靜態檔案,比片、office文檔等。A網站的操作需要上傳檔案到B網站,

下面介紹一種方法通過System.Net.WebClient類的UploadData方法 。

 

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 後台代碼

 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(); //尾碼名                    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); //讀取流之後設定設定當前流的位置,因為md5加密也需要用到該流                    string md5Value =  Com.MD5.MyMD5.getMd5Hash(item.InputStream); //得到上傳檔案流的MD5string url = string.Format("http://localhost:4884/SaveFile.ashx?name=" + md5Value + "&ext=" + fileDoc);  //儲存流的處理檔案路徑                    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); //擷取SaveFile.ashx返回                }            }        }
Com.MD5.MyMD5.getMd5Hash方法請看:我的C# MD5摘要演算法、雜湊演算法

SaveFile.ashx 處理常式

 /// <summary>    /// 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);//設定當前流的位置                            //Request.InputStream.Position = 0;                            Request.InputStream.Flush();                            Request.InputStream.Close();                            Request.InputStream.Dispose();                            Random rnd = new Random();                            int n = rnd.Next(1000, 9999);                            //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;            }        }    }




 

 

 

Asp.net上傳檔案後台通過二進位流發送到其他Url儲存

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.