C#實(shí)現(xiàn)HTTP上傳文件的方法
本文實(shí)例講述了C#實(shí)現(xiàn)HTTP上傳文件的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
發(fā)送文件代碼如下:
/// <summary>
/// Http上傳文件
/// </summary>
public static string HttpUploadFile(string url, string path)
{
// 設(shè)置參數(shù)
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
CookieContainer cookieContainer = new CookieContainer();
request.CookieContainer = cookieContainer;
request.AllowAutoRedirect = true;
request.Method = "POST";
string boundary = DateTime.Now.Ticks.ToString("X"); // 隨機(jī)分隔線
request.ContentType = "multipart/form-data;charset=utf-8;boundary=" + boundary;
byte[] itemBoundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "\r\n");
byte[] endBoundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n");
int pos = path.LastIndexOf("\\");
string fileName = path.Substring(pos + 1);
//請(qǐng)求頭部信息
StringBuilder sbHeader = new StringBuilder(string.Format("Content-Disposition:form-data;name=\"file\";filename=\"{0}\"\r\nContent-Type:application/octet-stream\r\n\r\n", fileName));
byte[] postHeaderBytes = Encoding.UTF8.GetBytes(sbHeader.ToString());
FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
byte[] bArr = new byte[fs.Length];
fs.Read(bArr, 0, bArr.Length);
fs.Close();
Stream postStream = request.GetRequestStream();
postStream.Write(itemBoundaryBytes, 0, itemBoundaryBytes.Length);
postStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);
postStream.Write(bArr, 0, bArr.Length);
postStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);
postStream.Close();
//發(fā)送請(qǐng)求并獲取相應(yīng)回應(yīng)數(shù)據(jù)
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
//直到request.GetResponse()程序才開(kāi)始向目標(biāo)網(wǎng)頁(yè)發(fā)送Post請(qǐng)求
Stream instream = response.GetResponseStream();
StreamReader sr = new StreamReader(instream, Encoding.UTF8);
//返回結(jié)果網(wǎng)頁(yè)(html)代碼
string content = sr.ReadToEnd();
return content;
}
接收文件的代碼如下:
using System;
using System.Web;
namespace SWX
{
public partial class test2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
HttpPostedFile file = Request.Files[0];
file.SaveAs(MapPath("\\UploadFile\\" + file.FileName));
Response.Write("Success\r\n");
}
}
}
希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。
相關(guān)文章
C# 繪制統(tǒng)計(jì)圖大全(柱狀圖, 折線圖, 扇形圖)
本篇文章介紹了C# 繪制統(tǒng)計(jì)圖大全,其中包括狀圖, 折線圖, 扇形圖,有需要的同學(xué)可以了解一下。2016-11-11
Winform啟動(dòng)另一個(gè)項(xiàng)目傳值的方法
這篇文章主要介紹了Winform啟動(dòng)另一個(gè)項(xiàng)目傳值的方法,通過(guò)調(diào)用進(jìn)程來(lái)實(shí)現(xiàn)項(xiàng)目之間的傳值,需要的朋友可以參考下2014-11-11
C#實(shí)現(xiàn)仿QQ抽屜式窗體的設(shè)計(jì)方法
QQ軟件對(duì)于絕大多數(shù)的人來(lái)說(shuō)再熟悉不過(guò)了,它以使用方便、界面美觀及功能完善而著稱(chēng),本文給大家介紹了C#實(shí)現(xiàn)仿QQ抽屜式窗體的設(shè)計(jì)方法,主要通過(guò)使用API函數(shù)WindowFromPoint和GetParent實(shí)現(xiàn)仿QQ的抽屜式窗體,需要的朋友可以參考下2024-04-04
C# 特性AttributeUsage簡(jiǎn)介與使用教程
這篇文章主要介紹了C# 特性AttributeUsage簡(jiǎn)介與使用教程,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-05-05
時(shí)間戳與時(shí)間相互轉(zhuǎn)換(php .net精確到毫秒)
本文給大家分享的時(shí)間戳與時(shí)間相互轉(zhuǎn)換(php .net精確到毫秒) ,感興趣的朋友一起學(xué)習(xí)吧2015-09-09
C#解碼base64編碼二進(jìn)制數(shù)據(jù)的方法
這篇文章主要介紹了C#解碼base64編碼二進(jìn)制數(shù)據(jù)的方法,涉及C#中Convert類(lèi)的靜態(tài)方法Convert.FromBase64String使用技巧,需要的朋友可以參考下2015-04-04
C#使用ODBC與OLEDB連接數(shù)據(jù)庫(kù)的方法示例
這篇文章主要介紹了C#使用ODBC與OLEDB連接數(shù)據(jù)庫(kù)的方法,結(jié)合實(shí)例形式分析了C#基于ODBC與OLEDB實(shí)現(xiàn)數(shù)據(jù)庫(kù)連接操作簡(jiǎn)單操作技巧,需要的朋友可以參考下2017-05-05

