C# ASPX FTP 上传后文件大小错误
我正在通过 FTP 上传一个 613.6 kb 的文件,它是一个 PDF 文件。当它到达服务器时,它现在是 82 KB。
在我的 Web.Config 中,我
<httpRuntime executionTimeout="7200" maxRequestLength="2097151" requestValidationMode="2.0"/>
的代码是这样的,
void UploadFileToFtp(FileInfo file, FtpWebRequest req)
{
int buffLength = 2048;
using (var reader = new BinaryReader(file.OpenRead(), Encoding.Default))
{
using (var writer = new BinaryWriter(req.GetRequestStream()))
{
while (reader.PeekChar() > 0) writer.Write(reader.ReadBytes(buffLength));
writer.Flush();
writer.Close();
}
reader.Close();
}
}
我认为它与编码有关,所以我尝试了 ASCII、默认和 Unicode,但没有任何区别。我添加了同花顺和关闭,以为可以做到这一点,但也没有帮助。我收到有关编码的错误,一旦我添加错误就消失了,但我没有其他错误。
I am uploading a file through FTP that is 613.6 kb, it is a PDF file. When it arrives on the server it is now 82 KB.
in my Web.Config I have
<httpRuntime executionTimeout="7200" maxRequestLength="2097151" requestValidationMode="2.0"/>
my Code is this
void UploadFileToFtp(FileInfo file, FtpWebRequest req)
{
int buffLength = 2048;
using (var reader = new BinaryReader(file.OpenRead(), Encoding.Default))
{
using (var writer = new BinaryWriter(req.GetRequestStream()))
{
while (reader.PeekChar() > 0) writer.Write(reader.ReadBytes(buffLength));
writer.Flush();
writer.Close();
}
reader.Close();
}
}
I thought it had something to do with the Encoding so I tried ASCII, Default and Unicode none of which made a diffrence. I added the Flush and the Close thinking that was doing it, but no help either. I was getting an error regarding the encoding and once I added that the error went away, but I have no other errors.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是否该文件包含零,并且您的比较应该是
reader.PeekChar() >=0
而不是reader.PeekChar() > 0?
Could it be that the file contains zeros and your comparison should be
reader.PeekChar() >=0
rather thanreader.PeekChar() > 0
?您真的需要使用
BinaryWriter
和BinaryReader
吗?它们可能会进行某种缓冲,我不相信您需要这样做。尝试这段代码,看看会发生什么Do you really need to use
BinaryWriter
andBinaryReader
, they are likely doing some kind of buffering I am not convinced you need that. Try this code and see what happens