将文件上传到FTP目的地已损坏,一旦 [英] Uploading files to FTP are corrupted once in destination
问题描述
我创建一个简单的拖放文件和上传 - 自动到FTP windows应用程序
和我使用的 MSDN代码上传文件到FTP。
中的代码是相当平直前锋:
//获取用于与服务器通信的对象。
的FtpWebRequest请求=(的FtpWebRequest)WebRequest.Create(的String.Format({0} {1},FTP_PATH,filenameToUpload));
request.Method = WebRequestMethods.Ftp.UploadFile;
//选项
request.UseBinary = TRUE;
request.UsePassive = FALSE;
// FTP凭证
request.Credentials =新的NetworkCredential(FTP_USR,FTP_PWD);
//文件的内容复制到请求流。
的StreamReader的SourceStream =新的StreamReader(fileToUpload.FullName);
字节[] = fileContents Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
sourceStream.Close();
request.ContentLength = fileContents.Length;
流requestStream = request.GetRequestStream();
requestStream.Write(fileContents,0,fileContents.Length);
requestStream.Close();
FtpWebResponse响应=(FtpWebResponse)request.GetResponse();
writeOutput(上传文件完成!);
writeOutput(状态:+ response.StatusDescription);
response.Close();
和确实获取上传到FTP
问题是当我看到在浏览器上的文件,或者只需下载并尝试看到它在桌面上,我得到:
我已经用 request.UseBinary = FALSE;
和 request.UsePassive = FALSE;
,但它不缝做任何形式的任何好。
我已经找到退出是,原来的文件有122KB lenght并在FTP(和下载后),它有219KB。 ..
我在做什么错了?
块引用>
顺便说一句,在
uploadFileToFTP()
方法在的BackgroundWorker
运行,但我真的不有什么差别的事情...
解决方案您不应该使用一个StreamReader,但只有一个流中读取二进制文件。< 。/ p>
StreamReader的设计为只读文本文件
试试这个:
私有静态无效了(字符串的资源文件,字符串的TargetFile)
{
试
{
串ftpServerIP = ConfigurationManager中.AppSettings [ftpIP];
串ftpUserID = ConfigurationManager.AppSettings [名为ftpuser];
串ftpPassword = ConfigurationManager.AppSettings [ftpPass];
////字符串ftpURI =;
字符串文件名=FTP://+ ftpServerIP +//+的TargetFile;
的FtpWebRequest ftpReq =(的FtpWebRequest)WebRequest.Create(文件名);
ftpReq.UseBinary = TRUE;
ftpReq.Method = WebRequestMethods.Ftp.UploadFile;
ftpReq.Credentials =新的NetworkCredential(ftpUserID,ftpPassword);
字节[] B = File.ReadAllBytes(的资源文件);
ftpReq.ContentLength = b.length个;
使用(流S = ftpReq.GetRequestStream())
{
s.Write(B,0,b.length个);
}
FtpWebResponse ftpResp =(FtpWebResponse)ftpReq.GetResponse();
如果(ftpResp!= NULL)
{
MessageBox.Show(ftpResp.StatusDescription);
}
}
赶上(异常前)
{
MessageBox.Show(ex.ToString());
}
}
I'm creating a simple drag-file-and-upload-automatically-to-ftp windows application
and I'm using the MSDN code to upload the file to the FTP.
The code is pretty straight forward:
// Get the object used to communicate with the server. FtpWebRequest request = (FtpWebRequest)WebRequest.Create(String.Format("{0}{1}", FTP_PATH, filenameToUpload)); request.Method = WebRequestMethods.Ftp.UploadFile; // Options request.UseBinary = true; request.UsePassive = false; // FTP Credentials request.Credentials = new NetworkCredential(FTP_USR, FTP_PWD); // Copy the contents of the file to the request stream. StreamReader sourceStream = new StreamReader(fileToUpload.FullName); byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd()); sourceStream.Close(); request.ContentLength = fileContents.Length; Stream requestStream = request.GetRequestStream(); requestStream.Write(fileContents, 0, fileContents.Length); requestStream.Close(); FtpWebResponse response = (FtpWebResponse)request.GetResponse(); writeOutput("Upload File Complete!"); writeOutput("Status: " + response.StatusDescription); response.Close();
and it does get uploaded to the FTP
Problem is when I see the file on a browser, or simply download and try to see it on desktop I get:
I already used
request.UseBinary = false;
andrequest.UsePassive = false;
but it does not seam to do any kind of good whatsoever.What I have found out was that, the original file has 122Kb lenght and in the FTP (and after downloading), it has 219Kb...
What am I doing wrong?
By the way, the
uploadFileToFTP()
method is running inside aBackgroundWorker
, but I don't really thing that makes any difference...解决方案You shouldn't use a StreamReader but only a Stream to read binary files.
Streamreader is designed to read text files only.
Try with this :
private static void up(string sourceFile, string targetFile) { try { string ftpServerIP = ConfigurationManager.AppSettings["ftpIP"]; string ftpUserID = ConfigurationManager.AppSettings["ftpUser"]; string ftpPassword = ConfigurationManager.AppSettings["ftpPass"]; ////string ftpURI = ""; string filename = "ftp://" + ftpServerIP + "//" + targetFile; FtpWebRequest ftpReq = (FtpWebRequest)WebRequest.Create(filename); ftpReq.UseBinary = true; ftpReq.Method = WebRequestMethods.Ftp.UploadFile; ftpReq.Credentials = new NetworkCredential(ftpUserID, ftpPassword); byte[] b = File.ReadAllBytes(sourceFile); ftpReq.ContentLength = b.Length; using (Stream s = ftpReq.GetRequestStream()) { s.Write(b, 0, b.Length); } FtpWebResponse ftpResp = (FtpWebResponse)ftpReq.GetResponse(); if (ftpResp != null) { MessageBox.Show(ftpResp.StatusDescription); } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } }
这篇关于将文件上传到FTP目的地已损坏,一旦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!