在输入类型文件上使用C#上载文件

时间:2014-05-03 20:36:17

标签: c# html asp.net upload

我正在尝试弄清楚如何在

上使用C#上传文件
<input type="file" />

我一直在使用此示例上传表单http://cgi-lib.berkeley.edu/ex/fup.html测试我的代码。

以下是我从这个答案中获取的代码:https://stackoverflow.com/a/2996904/2245648

    static void Main(string[] args)
    {
        NameValueCollection nvc = new NameValueCollection();
        nvc.Add("tbxEmail", "test");
        nvc.Add("btn-submit-photo", "Upload");
        HttpUploadFile(@"http://jobview.monster.ca/Apply/Apply.aspx?JobID=133227319",
             @"C:\test\test.jpg", "file", "image/jpeg", nvc);
        Console.ReadLine();
    }



    public static void HttpUploadFile(string url, string file, string paramName, string contentType, NameValueCollection nvc)
    {
        Console.WriteLine(string.Format("Uploading {0} to {1}", file, url));
        string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
        byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");

        HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url);
        wr.ContentType = "multipart/form-data; bsundary=" + boundary;
        wr.Method = "POST";
        wr.KeepAlive = true;
        wr.Credentials = System.Net.CredentialCache.DefaultCredentials;

        Stream rs = wr.GetRequestStream();

        string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}";
        foreach (string key in nvc.Keys)
        {
            rs.Write(boundarybytes, 0, boundarybytes.Length);
            string formitem = string.Format(formdataTemplate, key, nvc[key]);
            byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem);
            rs.Write(formitembytes, 0, formitembytes.Length);
        }
        rs.Write(boundarybytes, 0, boundarybytes.Length);

        string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n";
        string header = string.Format(headerTemplate, paramName, file, contentType);
        byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
        rs.Write(headerbytes, 0, headerbytes.Length);

        FileStream fileStream = new FileStream(file, FileMode.Open, FileAccess.Read);
        byte[] buffer = new byte[4096];
        int bytesRead = 0;
        while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
        {
            rs.Write(buffer, 0, bytesRead);
        }
        fileStream.Close();

        byte[] trailer = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");
        rs.Write(trailer, 0, trailer.Length);
        rs.Close();

        WebResponse wresp = null;
        try
        {
            wresp = wr.GetResponse();
            Stream stream2 = wresp.GetResponseStream();
            StreamReader reader2 = new StreamReader(stream2);
            Console.WriteLine(string.Format("File uploaded, server response is: {0}", reader2.ReadToEnd()));
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error uploading file", ex);
            if (wresp != null)
            {
                wresp.Close();
                wresp = null;
            }
        }
        finally
        {
            wr = null;
        }
    }

我不知道在nvc.Add()放什么,我以为它会是元素的名字?如果我将文件上传到cgi-lib.berkeley.edu,这将如何运作?我看看我的结果,我再次得到了同样的页面。我假设我会在确认页面显示我的文件内容。

修改

WebClient是否支持multipart/form-data

0 个答案:

没有答案