最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#實(shí)現(xiàn)HTTP上傳文件的方法

 更新時(shí)間:2014年11月04日 09:15:50   投稿:shichen2014  
這篇文章主要介紹了C#實(shí)現(xiàn)HTTP上傳文件的方法,包括了發(fā)送文件與接收文件的實(shí)現(xiàn)代碼,具有不錯(cuò)的參考價(jià)值,需要的朋友可以參考下

本文實(shí)例講述了C#實(shí)現(xiàn)HTTP上傳文件的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:

發(fā)送文件代碼如下:

復(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ī)分隔線(xiàn)
    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;
}

接收文件的代碼如下:

復(fù)制代碼 代碼如下:

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)文章

最新評(píng)論

尼勒克县| 栾城县| 乌恰县| 郸城县| 贞丰县| 大兴区| 蓬安县| 阳西县| 巴青县| 峨边| 清新县| 扶沟县| 孝昌县| 望谟县| 遂昌县| 安国市| 清徐县| 保靖县| 杨浦区| 巴楚县| 繁昌县| 凯里市| 石台县| 南郑县| 绥中县| 长岛县| 萍乡市| 迁西县| 高雄县| 错那县| 盐源县| 江阴市| 德州市| 碌曲县| 元朗区| 武安市| 得荣县| 罗江县| 洞头县| 钟祥市| 苏州市|