将文件上传到FTP目的地已损坏,一旦

编程入门 行业动态 更新时间:2024-10-11 17:29:14
本文介绍了将文件上传到FTP目的地已损坏,一旦的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我创建一个简单的拖放文件和上传 - 自动到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,但只有一个流中读取二进制文件。

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.Co​​ntentLength = 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; and request.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 a BackgroundWorker, 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目的地已损坏,一旦

本文发布于:2023-11-24 23:30:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1627346.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件上传   目的地   FTP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!