该文件未使用HttpWebRequest上传

时间:2019-05-28 05:04:12

标签: c# file-upload httpwebrequest

我知道那里有几篇类似的文章,但我几乎都尝试过。我正在尝试使用HttpWebRequest在网站上上传图像。 代码运行没有错误,服务器的响应与手动上传的响应相同。问题是它代替了照片,而是显示了类似于下图的黑十字

enter image description here

我不知道出了什么问题以及什么原因导致了问题,我最好的猜测是问题可能出在我在服务器上读写文件的方式上。 这是我的代码

public static void HttpUploadFile(string url, string file, string paramName, string contentType, NameValueCollection nvc)
    {

        MessageBox.Show(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");


        CookieContainer cookieContainer = LoginToUrl();//this is to just pass the cookies; 



        HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url);
        wr.CookieContainer = cookieContainer;
        wr.ContentType = "multipart/form-data; boundary=" + boundary;
        wr.Method = "POST";
        wr.KeepAlive = true;

        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);
            MessageBox.Show(string.Format("File uploaded, server response is: {0}", reader2.ReadToEnd()));
        }
        catch (Exception ex)
        {
            MessageBox.Show(string.Format("Error uploading file", ex));
            if (wresp != null)
            {
                wresp.Close();
                wresp = null;
            }
        }
        finally
        {
            wr = null;
        }
    }

这就是我所说的:

private void UploadFile()
    {

        string url="https://portal.com/index.php?r=location/file";
        string file = "C:\\Vbn.jpg";
        string paramName= "file";
        string contentType = "image/jpeg";
        NameValueCollection nvc = new NameValueCollection();
        nvc.Add("rowid", "");
        nvc.Add("model", "Photo");
        nvc.Add("category", "LeadIn");
        nvc.Add("row", "");
        nvc.Add("oper", "new");
        nvc.Add("model_id", "2262");
        nvc.Add("max_image", "0");
        nvc.Add("type", "[Lead-in]Building Lead-in (from inside showing no of lead-in and cable currently housed)");



        HttpUploadFile(url, file, paramName, contentType, nvc);
    }

下图显示了来自IE的请求正文。 enter image description here

0 个答案:

没有答案