在ftp服务器上传时文件更大 [英] File Bigger when uploading on ftp server
本文介绍了在ftp服务器上传时文件更大的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从我的应用程序上传文件到ftp服务器。
但是当上传完成时,上传的文件比原来的文件要大
我发现了如果我有1024字节的缓冲区并且在最后一轮只有512被占用,它还将剩余的0512字节写入文件
这是我的代码:
I want to upload a file to a ftp server from my application.
But when upload completes, the file uploaded is bigger than the original one
I have discovered that if I have of buffer of 1024 bytes and on the last turn only 512 are occupied, it also writes the rest "0" 512 bytes to the file
Here is my code:
public bool UploadArticol(String adresaserver,String numefisier)
{
try
{
FileInfo toupload = new FileInfo("articles\\"+numefisier+".dat");
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://"+adresaserver+"/" + toupload.Name);
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential("lucian", "lucian");
Stream ftpstream = request.GetRequestStream();
FileStream file = File.OpenRead("articles\\"+numefisier+".dat");
int length = 1048576;
byte[] buffer = new byte[length];
int bytesread = 0;
do
{
bytesread = file.Read(buffer, 0, length);
ftpstream.Write(buffer, 0, length);
}
while (bytesread != 0);
file.Flush();
file.Close();
ftpstream.Close();
Console.WriteLine("Upload complete");
}
catch (Exception ee)
{
Console.WriteLine(ee.Message);
return false;
}
return true;
}
推荐答案
无论实际读取多少数据,您总是以1048576字节为单位写入数据。 />
两种选择:
1.将Write()更改为:
You are always writing the data in blocks of 1048576 bytes no matter how many are actually read.
Two choices:
1. Change the Write() to be:
ftpstream.Write(buffer, 0, bytesread);
2.只需拥有两个流直接交易:
2. Just have the two streams deal with each other directly:
public bool UploadArticol(String adresaserver, String numefisier)
{
string uploadname = "articles\\"+numefisier+".dat";
try
{
FileInfo toupload = new FileInfo(uploadname);
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://"+adresaserver+"/" + toupload.Name);
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential("lucian", "lucian");
Stream ftpstream = request.GetRequestStream();
using (FileStream file = File.OpenRead(uploadname))
{
file.CopyTo(ftpstream);
}
Console.WriteLine("Upload complete");
}
catch (Exception ee)
{
Console.WriteLine(ee.Message);
return false;
}
return true;
}
你可能还需要在流对象上使用Flush()方法...
如果你还有问题,请参考这些链接..
http://www.digitalcoding.com/Code-Snippets/C-Sharp/C-Code-Snippet-Upload-file-to-FTP-Server.html [< a href =http://www.digitalcoding.com/Code-Snippets/C-Sharp/C-Code-Snippet-Upload-file-to-FTP-Server.htmltarget =_ blanktitle =New Window > ^ ]
http://blog.pbdesk.com/2009/03/uploading-files-to-ftp-server-using.html [ ^ ]
you may need to use Flush() method on stream object as well...
if you have still issues, refer these links..
http://www.digitalcoding.com/Code-Snippets/C-Sharp/C-Code-Snippet-Upload-file-to-FTP-Server.html[^]
http://blog.pbdesk.com/2009/03/uploading-files-to-ftp-server-using.html[^]
这篇关于在ftp服务器上传时文件更大的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文