我最近不得不写一些代码:
byte[]
上传到网站(使用HttpWebRequest
。ContentType
为multipart/form-data
)byte[]
存储在数据库中。然后,此图像将用于报告,用户可以随时下载图像。
这工作正常,直到我们开始在Windows XP机器上测试它。每当我从XP电脑上传任何图像时,图像都不会显示。经过一些调试和测试并将此multipart/form-data
写入文本文件后,我发现文件的byte[]
在Windows XP与Windows 8(甚至7)上有所不同。生成的文件大小也不同。
我正在使用VS2012和.Net 4.0,并在XP电脑上安装(并再次修复).Net 4。 我只能认为两个操作系统编码方式不同,或者可能是32位操作系统和64位操作系统之间的差异。显然我不知道出了什么问题,甚至不知道从哪里开始。我想知道是否有人可以指出我正确的方向?
以下是UI端代码:
//Wrapped around each parameter. The last boundary after the byte[] file >has been omitted.
string boundary = "----------------------------" + >DateTime.Now.Ticks.ToString("x");
//Creating the httpWebRequest as multipart with "POST" method.
httpWebRequest = (HttpWebRequest)WebRequest.Create(_webUploadUrl);
httpWebRequest.ContentType = "multipart/form-data; boundary=" + boundary;
httpWebRequest.Method = "POST";
httpWebRequest.KeepAlive = true;
httpWebRequest.Credentials = >System.Net.CredentialCache.DefaultCredentials;
//Upload stream will be built with all the parameters, followed by the >byte[] file.
Stream uploadStream = new System.IO.MemoryStream();
byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes(boundary + >"\r\n");
string formdataTemplate = "\r\n--" + boundary +
"\r\nContent-Disposition: form-data; name=\"{0}\";\r\n\r\n{1}";
//Parameters:
//Foreach parameter, Wrap in boundary
foreach (string key in _nvcParameters.Keys)
{
string formitem = string.Format(formdataTemplate, key, >_nvcParameters[key]);
byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem);
uploadStream.Write(formitembytes, 0, formitembytes.Length);
}
byte[] netBytes = System.Text.Encoding.ASCII.GetBytes("\r\n--");
uploadStream.Write(netBytes, 0, netBytes.Length);
//The actual file:
uploadStream.Write(boundarybytes, 0, boundarybytes.Length);
string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; >filename=\"{1}\"\r\n Content-Type: application/octet-stream\r\n\r\n";
string header = string.Format(headerTemplate, "uplTheFile", _fileName);
byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
uploadStream.Write(headerbytes, 0, headerbytes.Length);
//While file is greater than buffer, write to uploadStream.
byte[] buffer = new byte[1024];
int bytesRead = 0;
long fileSize = pFileStream.Length;
long uploadedValue = 0;
while ((bytesRead = (pFileStream).Read(buffer, 0, buffer.Length)) != 0)
{
uploadStream.Write(buffer, 0, bytesRead);
Application.DoEvents();
}
httpWebRequest.ContentLength = uploadStream.Length;
//Close the original fileStream.
pFileStream.Close();
该网站使用UTF8。如果需要,我可以发布它,但由于byte[]
不同,我认为问题可能存在。
fileSize
和uploadedValue
变量吗?代码是公司UI系统的一部分,非常大,我只是开始了解我的方式,无论如何,我已经检查了项目的属性,(如果这是正确的检查位置)。在构建时,Platform目标是x86。你在谈论那个或其他什么?对不起,我还是一名学生,也是c#的新手(和那个问题的对手)
我在Windows 8机器上打开了这两个文件。 win8文件大小增加了6kb,显然包含更多字符。
这是数组的前几行。 _ncvParameters
是相同的(以及上传的文件,以下是生成的多部分表单的第一部分(ncvParameters
后跟byte[]
文件的前几行):
Win8:
------------------------------ 8d00a632401f30e Content-Disposition:form-data; NAME = “○”;
25 ------------------------------ 8d00a632401f30e Content-Disposition:form-data; NAME = “K”;
2913 ------------------------------ 8d00a632401f30e Content-Disposition:form-data; NAME = “U”;
255 ------------------------------ 8d00a632401f30e Content-Disposition:form-data; NAME = “F”;
Blue hills.jpg ------------------------------ 8d00a632401f30e Content-Disposition:form-data; NAME = “M”;
图像/ JPEG ------------------------------ 8d00a632401f30e Content-Disposition:form-data; NAME = “uplTheFile”; filename =“Blue hills.jpg”内容类型: 应用/八位字节流
‰PNG
IHDR € à 5ÑÜä sRGB ®Îé gAMA ±üa pHYs à ÃÇo¨d ÿ¥IDATx^ìýg—\7²ëÿ÷¾gïî–§÷¤H9Š¢DR”—Ú{Ç6²”÷M‘yç¹V$™UÅ–úô>÷|˜@
~ÌÄ2y×?oݾøáíÛWþ±OY†[瓦特€“õøû-pëÊßnï㺷AO‡uxK¾í
WinXP:
------------------------------ 8d00a639ff7bf76 Content-Disposition:form-data; NAME = “○”;
25 ------------------------------ 8d00a639ff7bf76 Content-Disposition:form-data; NAME = “K”;
2913 ------------------------------ 8d00a639ff7bf76 Content-Disposition:form-data; NAME = “U”;
255 ------------------------------ 8d00a639ff7bf76 Content-Disposition:form-data; NAME = “F”;
Blue hills.jpg ------------------------------ 8d00a639ff7bf76 Content-Disposition:form-data; NAME = “M”;
图像/ JPEG ------------------------------ 8d00a639ff7bf76 Content-Disposition:form-data; NAME = “uplTheFile”; filename =“Blue hills.jpg”内容类型: 应用/八位字节流
‰PNG
IHDR ĉ sRGB ®Îé gAMA ±üa cHRM z& €„ ú €è u0 ê` :˜ pœºQ< IDATWc` ªÕÈQ
IEND®B`,'èÊû:uÊžòÞ°E [=)Qä¡w¢%º2§Î〜󙉬½×F±¤〜&OverBar;×1‰H $ 01#ùßÿâ<ÿ¯¿¸äÿý ‡<-U;üX§¿8(YQ $º$ A“ÿþéÚêÀBTÿpà%•〜ÖbºËá þÝü8ùŸ:å_ø(IÿGã<þâ/ÆCô¨Í。*> QV
@Xaqron - uploadStream是memoryStream
。整个multipart-form将写入此流,然后写入byte[]
。哪个流式传输到httpWebRequest
。
我很确定它的图像是一样的。为了确保我已经尝试了两次。今天我将在办公室进行三重检查 - 是的,我现在已多次完成。相同的图像会产生不同的大小和不同的byte[]
个字符。
好的,我想我已经找到了问题。这可能是有用的信息,但我甚至认为这不是问题。上传的文件是一张图片,我调整了大小。调整大小方法需要byte[]
(我认为编码错误的那个),将其写入位图。然后,使用PixelFormat.Format32bppRgb
和InterpolationMode.HighQualityBicubic
将此位图“重新绘制”为新尺寸:
if (pImage.Height < maxHeight && pImage.Width < maxWidth) return pImage;
using (pImage)
{
Double xRatio = (double)pImage.Width / maxWidth;
Double yRatio = (double)pImage.Height / maxHeight;
Double ratio = Math.Max(xRatio, yRatio);
int nnx = (int)Math.Floor(pImage.Width / ratio);
int nny = (int)Math.Floor(pImage.Height / ratio);
System.Drawing.Bitmap cpy = new System.Drawing.Bitmap(nnx, nny, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
using (System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(cpy))
{
gr.Clear(System.Drawing.Color.Transparent);
gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
gr.DrawImage(pImage,
new System.Drawing.Rectangle(0, 0, nnx, nny),
new System.Drawing.Rectangle(0, 0, pImage.Width, pImage.Height),
System.Drawing.GraphicsUnit.Pixel);
}
return cpy;
}
我将PixelFormat
模式更改为'DontCare',将InterpolationMode
模式更改为Default
。
对不起,这本来是显而易见的,但我认为这是由.net框架处理的?
答案 0 :(得分:1)
Windows XP以及像7和8这样的新版本的编码和解码方式不同。我可以为您推荐的是在传输之前将您的字节数组编码到BASE64中。然后接收它并解码回你的字节数组。 .Net有简单的方法来编码和解码base64。由于base64文本是7位ascii,因此传输它没有问题。