WebClient上傳圖片到遠程服務的程式碼範例

來源:互聯網
上載者:User
這篇文章主要介紹了ASP.NET中利用WebClient上傳圖片到遠程服務的方法,包括用戶端和服務端,代碼附有注釋,需要的的朋友參考下吧

一、用戶端

1.頁面

<form id="Form1" method="post" runat="server" enctype="multipart/form-data">    <input id="MyFile" type="file" runat="server" />    <br />    <br />    <asp:Button ID="Button1" runat="server" Text="上傳檔案" OnClick="Button1_Click"></asp:Button>  </form>

2.後台

System.Web.HttpFileCollection oFiles = System.Web.HttpContext.Current.Request.Files;   string FilePath = oFiles[0].FileName;   string FileName = FilePath.Substring(FilePath.LastIndexOf("\\") + 1);   byte[] b = new byte[oFiles[0].ContentLength];   System.IO.Stream fs = (System.IO.Stream)oFiles[0].InputStream;   fs.Read(b, 0, oFiles[0].ContentLength);   string postData = "data=" + HttpUtility.UrlEncode(Convert.ToBase64String(b));   var webclient = new WebClient();   webclient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");   byte[] byteArray = Encoding.UTF8.GetBytes(postData);   //byte[] buffer = webclient.UploadData("http://localhost/datapush/DataPush.ashx", "POST", byteArray);//ashx   byte[] buffer = webclient.UploadData("http://localhost/datapush/WebServiceDataPush.asmx/DataPush", "POST", byteArray);//asmx   var msg = Encoding.UTF8.GetString(buffer);   Response.Write(msg);

二、服務端

string msg = "";   byte[] filedata = Convert.FromBase64String(context.Request["data"]);   if (filedata.Length == 0)   {    msg= "{\"code\":\"上傳的是空檔案\"}";   }   if (filedata.Length > 1048576)   {    msg = "{\"code\":\"圖片大小不能超過1M\"}";   }   string fileextension = filedata[0].ToString() + filedata[1].ToString();   if (fileextension == "7173")   {    fileextension = "gif";   }   else if (fileextension == "255216")   {    fileextension = "jpg";   }   else if (fileextension == "13780")   {    fileextension = "png";   }   else if (fileextension == "6677")   {    fileextension = "bmp";   }   else if (fileextension == "7373")   {    fileextension = "tif";   }   else   {    msg = "{\"code\":\"上傳的檔案不是圖片\"}";   }   try   {    //儲存圖片    string filename = Guid.NewGuid().ToString("D") + "." + fileextension;    System.IO.MemoryStream ms = new System.IO.MemoryStream(filedata);    System.IO.FileStream fs = new System.IO.FileStream(context.Server.MapPath("~/") + "/採集圖片/" + filename, System.IO.FileMode.Create);    ms.WriteTo(fs);    ms.Close();    fs.Close();    fs = null;    ms = null;    msg = "{\"code\":\"上傳圖片成功\"}";   }   catch (Exception exe)   {    msg = "{\"code\":\"" + exe.Message + "\"}";   }

【相關推薦】

1. ASP免費視頻教程

2. ASP教程

3. 李炎恢ASP基礎視頻教程

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.