我的代码从 Windows Mobile 将文件上传到服务器的问题在哪里
我在从 Windows Mobile 上传 zip 文件到服务器时遇到问题。 在服务器中,正在创建 .zip 文件,如果我打开文件,它会告诉我无法打开并且它已损坏,
这里是代码
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uploadUrl);
req.Method = "PUT";
req.AllowWriteStreamBuffering = true;
// Retrieve request stream and wrap in StreamWriter
Stream reqStream = req.GetRequestStream();
StreamWriter wrtr = new StreamWriter(reqStream);
// Open the local file
StreamReader rdr = new StreamReader(localFile);
// loop through the local file reading each line
char[] buff = new char[1024];
int inLine = rdr.Read(buff, 0, 1024);
//int inLine = rdr.ReadBlock (buff,0,1024);
while (inLine > 0)
{
wrtr.WriteLine (buff);
inLine = rdr.Read (buff, 0, 1024);
}
rdr.Close();
wrtr.Close();
try
{
req.GetResponse();
}
catch
{
}
reqStream.Close();
谢谢
i have problem of uploading zip file ot server from my windows mobile..
in server the .zip file is getting created,if i open file its telling unable to open and its corrupted
here is code
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uploadUrl);
req.Method = "PUT";
req.AllowWriteStreamBuffering = true;
// Retrieve request stream and wrap in StreamWriter
Stream reqStream = req.GetRequestStream();
StreamWriter wrtr = new StreamWriter(reqStream);
// Open the local file
StreamReader rdr = new StreamReader(localFile);
// loop through the local file reading each line
char[] buff = new char[1024];
int inLine = rdr.Read(buff, 0, 1024);
//int inLine = rdr.ReadBlock (buff,0,1024);
while (inLine > 0)
{
wrtr.WriteLine (buff);
inLine = rdr.Read (buff, 0, 1024);
}
rdr.Close();
wrtr.Close();
try
{
req.GetResponse();
}
catch
{
}
reqStream.Close();
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我看到的第一个问题是,您总是写入“buff”的全部内容,而不仅仅是读取的字节数。除非你的文件大小能被 1024 整除,否则这是一个问题。即使读取没有返回完整的 1024 字节,这也是一个问题。
更新1
我在这里问的第二件事是为什么使用 char[] 来保存二进制数据?这只是一种不好的做法。 char 用于字符串数据(CE 中的 char 无论如何都是 2 个字节)。使用 byte[] 表示二进制数据 - 这就是它的用途。当前的编码很可能正在执行诸如从数据字节中剥离 MSB 之类的操作,因为您将其填充到 char 中。这也可能与尺寸问题有关。
The first problem I see is that you're always writing the entire contents of 'buff', not just the number of bytes read. Unless you file size is evenly divisible by 1024 that's a problem. It's also a problem in the even the read doesn't bring back a full 1024 bytes.
Update 1
THe second thing I question here is why are you using a char[] for holding binary data? That's just a bad practice all around. char is for string data (and char in CE is 2 bytes anyway). Use a byte[] for binary data - that's what it's for. It's quite possible that the current encoding is doing something like stripping the MSB off of data bytes because you're stuffing it into a char. It could also have to do with the size issue.