如何将图像上传到服务器

如何将图像上传到服务器

本文介绍了如何将图像上传到服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想模仿这个请求

I want emulate this request

-----------------------------708299735697
Content-Disposition: form-data; name="_file"

1.jpg
-----------------------------708299735697
Content-Disposition: form-data; name="file"; filename="blob"
Content-Type: image/png

‰PNG
............





var taimalda = DateTime.Now.Ticks;
            var boundary = "------------------------" + taimalda ;
            var newLine = Environment.NewLine;
            var propFormat = "--" + boundary + newLine +
                                "Content-Disposition: form-data; name=\"{0}\"" + newLine + newLine +
                                "{1}" + newLine;
            var fileHeaderFormat = "--" + boundary + newLine +
                                    "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"" + newLine + "Content-Type: image/png";

            var req = (HttpWebRequest)HttpWebRequest.Create("http://xxx.ru/new_style/flash_uploader/upload.php?fileapi"+taimalda);
            req.CookieContainer = s;
            System.Net.ServicePointManager.Expect100Continue = false;
            req.Referer = "http://www.xxx.ru/user/setting/set_info";

            req.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:2.0) Gecko/20100101 Firefox/4.0";
            req.Method = WebRequestMethods.Http.Post;
            req.ContentType = "multipart/form-data; boundary=" + boundary;

            using (var reqStream = req.GetRequestStream())
            {
                var reqWriter = new StreamWriter(reqStream);
                var tmp = string.Format(propFormat, "_file", "1.jpg");
                reqWriter.Write(tmp);
                tmp = string.Format(fileHeaderFormat, "file", "blob");
                reqWriter.Write(tmp);
                reqWriter.Flush();
            }
            var res = req.GetResponse();
            using (var resStream = res.GetResponseStream())
            {
                var reader = new StreamReader(resStream);
                var ext = reader.ReadToEnd();
            }



但这段代码只发送了没有我文件的标题(1.jpg)


but this code sent only headers without my file(1.jpg)

--------------------------635031060420469298
Content-Disposition: form-data; name="_file"

1.jpg
--------------------------635031060420469298
Content-Disposition: form-data; name="file"; filename="blob"
Content-Type: image/png

推荐答案


FileStream fs = new FileStream("1.jpg", FileMode.Open, FileAccess.Read);
 int bufferSize = 40960; //Amount of data that to be read from file(in bytes)
 byte[] buffer = br.ReadBytes(bufferSize);//reading the bytes
......
req.ContentType = "multipart/form-data; boundary=" + boundary;

            using (var reqStream = req.GetRequestStream())
            {
                var reqWriter = new StreamWriter(reqStream);
                var tmp = string.Format(propFormat, "_file", "1.jpg");
                reqWriter.Write(tmp);
                tmp = string.Format(fileHeaderFormat, "file", "blob");
                reqWriter.Write(tmp);

                int ken = fs.Read(buffer, 0, buffer.Length);
                reqWriter.Write(buffer,0,ken); // cannot convert byte[] to char[]
                reqWriter.Flush();
            }


private byte[] GetBytes(string str)
{
    byte[] bytes = new byte[str.Length * sizeof(char)];
    System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
    return bytes;
}





稍微修改你的流代码:



Modify your stream code a bit:

FileStream fs = new FileStream("1.jpg", FileMode.Open, FileAccess.Read);
req.ContentType = "multipart/form-data; boundary=" + boundary;
 
using (var stream = req.GetRequestStream())
{
                
                var tmp = string.Format(propFormat, "_file", "1.jpg");
                stream.Write(GetBytes(tmp));
                tmp = string.Format(fileHeaderFormat, "file", "blob");
                stream.Write(GetBytes(tmp));
                stream.Flush();
                fs.CopyTo(stream);
                stream.Flush.Flush();
}

You need to convert string to bytes in order to be able to write to a Stream. You will need to discard StreamWriter as it won't allow to write bytes to a stream.


这篇关于如何将图像上传到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 10:02