Bez*_*eel 11 c# windows-xp windows-8
我最近不得不写一些代码:
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();
Run Code Online (Sandbox Code Playgroud)
该网站使用UTF8.如果需要我可以发布它,但因为byte[]s不同,我认为问题可能存在.
@Ramhound使用时间长?你指的是fileSize和uploadedValue变量?代码是公司UI系统的一部分,非常大,我只是开始了解我的方式,无论如何,我已经检查了项目的属性,(如果这是正确的检查位置).在构建时,Platform目标是x86.你在谈论那个或其他什么?对不起,我还是一名学生,也是c#的新手(和那个问题的vs)
我在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";
image/jpeg ------------------------------ 8d00a632401f30e Content-Disposition:form-data; NAME = "uplTheFile"; filename ="Blue hills.jpg"Content-Type:application/octet-stream
‰PNG
Run Code Online (Sandbox Code Playgroud)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";
image/jpeg ------------------------------ 8d00a639ff7bf76 Content-Disposition:form-data; NAME = "uplTheFile"; filename ="Blue hills.jpg"Content-Type:application/octet-stream
‰PNG
Run Code Online (Sandbox Code Playgroud)IHDR ĉ sRGB ®Îé gAMA ±üa cHRM z& €„ ú €è u0 ê` :˜ pœºQ< IDATWc` ªÕÈQIEND®B`,'èÊû:uÊžòÞ°E [=)Qä¡w¢%º2§Î〜󙉬½×F±¤〜‾×1‰H $ 01#ùßÿâ<ÿ¯¿¸äÿý‡< - ü;üX§¿8(QQ $?º$Ã"ÿþéÚêÀBTÿpà%•〜ÖbºËáþÝ8ùŸ:å_ø(IÿGã<þâ/ÆCô¨Í.*> QV
@Xaqron - uploadStream是一个memoryStream.整个multipart-form被写入此流,然后写入a 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;
}
Run Code Online (Sandbox Code Playgroud)
我将PixelFormat模式改为'DontCare'和InterpolationModeto Default.对不起,这本来是显而易见的,但我认为这是由.net框架处理的?
Windows XP 和较新的操作系统(例如 7 和 8)的编码和解码方式不同。我可以建议您的是在传输之前将字节数组编码为 BASE64。然后接收它并解码回您的字节数组。.Net 有简单的方法来编码和解码 Base64。由于 base64 文本是 7 位 ascii,因此传输它不会有问题。
| 归档时间: |
|
| 查看次数: |
1023 次 |
| 最近记录: |