基本信息
源码名称:asp.net 上传文件至wps
源码大小:0.02M
文件格式:.zip
开发语言:C#
更新时间:2017-03-13
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 1 元 
   源码介绍


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.IO;
using System.Collections.Specialized;
using System.Text;

public partial class _Default : System.Web.UI.Page 
{
    public string Result = "null";

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Submit_Click(object sender, EventArgs e)
    {
        if (PostFile.PostedFile != null)
        {
            var t = PostRequest(PostFile.PostedFile);
            Result = t;
        }
    }

    protected string PostRequest(HttpPostedFile file)
    {
        //请求地址
        string url =
            "http://view.wps.cn/service/dotview/process.php?act=upload";

        //创建HTTP Request对象
        HttpWebRequest req = WebRequest.Create(url) as HttpWebRequest;

        req.Method = "POST"; //设置请求方式为POST
        req.Accept = "text/*"; //设置HTTP标头的 Accept值为 text/*
        req.UserAgent = "Shockwave Flash"; //设置User-Agent标头的值
        //req.Connection = "Keep-Alive"; 
        SetHeaderValue(req.Headers, "Connection", "Keep-Alive"); //设置Connection标头的值
        string boundary = "cH2ei4gL6cH2GI3ei4KM7KM7Ij5gL6"; //创建边界码
        req.ContentType = "multipart/form-data; boundary=----------"   boundary; //设置Content-Type标头的值

        //创建请求实体第一部分
        StringBuilder reqBody1 = new StringBuilder("\r\n");
        reqBody1.AppendLine(string.Format("{0}{1}", "------------", boundary));
        reqBody1.AppendLine(string.Format("{0}", "Content-Disposition: form-data; name=\"Filename\""));
        reqBody1.AppendLine();
        reqBody1.AppendLine(string.Format("{0}", file.FileName));
        reqBody1.AppendLine(string.Format("{0}{1}", "------------", boundary));
        reqBody1.AppendLine(string.Format("Content-Disposition: form-data; name=\"Filedata\"; filename=\"{0}\"", file.FileName));
        reqBody1.AppendLine(string.Format("nContent-Type: application/octet-stream"));
        reqBody1.AppendLine();

        //将请求第一部分转换成字节序列
        var reqBody1Bytes = Encoding.UTF8.GetBytes(reqBody1.ToString());

        //读取文件内容转换成字节序列
        var reqBody2Bytes = new byte[file.ContentLength];
        file.InputStream.Read(reqBody2Bytes, 0, file.ContentLength);

        //创建请求实体第三部分
        StringBuilder reqBody3 = new StringBuilder();
        reqBody3.AppendLine(string.Format("{0}{1}", "------------", boundary));
        reqBody3.AppendLine(string.Format("Content-Disposition: form-data; name=\"{0}\"", "upload"));
        reqBody3.AppendLine();
        reqBody3.AppendLine("Submit Query");
        reqBody3.AppendLine(string.Format("------------{0}--", boundary));

        //将请求第三部分转换成字节序列
        var reqBody3Bytes = Encoding.UTF8.GetBytes(reqBody3.ToString());

        //设置Content-Length标头
        req.ContentLength = reqBody1Bytes.Length   reqBody2Bytes.Length   reqBody3Bytes.Length;


        //创建请求写入流,并将主体的各部分的字节序列写入http流
        Stream writer = req.GetRequestStream();
        writer.Write(reqBody1Bytes, 0, reqBody1Bytes.Length);
        writer.Write(reqBody2Bytes, 0, reqBody2Bytes.Length);
        writer.Write(reqBody3Bytes, 0, reqBody3Bytes.Length);

        //清除缓存,关闭流
        writer.Flush();
        writer.Close();

        //获取响应
        HttpWebResponse res = req.GetResponse() as HttpWebResponse;
        StreamReader sr = new StreamReader(res.GetResponseStream());
        string r = sr.ReadToEnd();
        sr.Close();
        return r;
    }

    public static void SetHeaderValue(WebHeaderCollection header, string name, string value)
    {
        var property = typeof(WebHeaderCollection).GetProperty("InnerCollection",
            System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
        if (property != null)
        {
            var collection = property.GetValue(header, null) as NameValueCollection;
            collection[name] = value;
        }
    }

    /// <summary>
    /// 获取一个指定长度的随机字符串
    /// </summary>
    /// <param name="len"></param>
    /// <returns></returns>
    public static string RandomString(int len)
    {
        string charArray = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

        char[] tmp = new char[len];
        Random random = new Random();

        for (var i = 0; i < len; i  )
        {
            tmp[i] = charArray[random.Next(charArray.Length)];
        }
        return new string(tmp);
    }
}