我的代码从 Windows Mobile 将文件上传到服务器的问题在哪里

发布于 2024-08-11 11:31:56 字数 1038 浏览 10 评论 0原文

我在从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

赠我空喜 2024-08-18 11:31:56

我看到的第一个问题是,您总是写入“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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文