如何将图像上传到服务器 [英] How to upload image to server

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

问题描述

我想模仿这个请求

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

推荐答案

您还没有将文件写入流!

在reqWriter.Write(tmp)写完您的完整数据后然后继续下一个流。
You haven''t written your file to the stream!
after reqWriter.Write(tmp) write your complete data and then continue with the next stream.


我想要写入图像数组来请求。此代码返回错误

I want write image array to request. This code return error
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();
            }


你还没有把文件写入流!

reqWriter.Write(tmp)之后写下您的完整数据然后继续下一个流。



编辑:这是您要求的代码。首先添加一个方法来帮助字符串转换为byte []



You haven''t written your file to the stream!
after reqWriter.Write(tmp) write your complete data and then continue with the next stream.

Here''s code as you asked for. First add a method to aid string conversion to byte[]

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.


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆