我在C#中编写了一个函数,用于在复制到文件共享时无法上传文件。我注意到任何上传的文件大小都是原始文件的1.5-2倍,并且是无效文件。这是代码:
public bool save_FTPUpload(FileInfo fi_attachment)
{
bool fileSaved = false;
string filename = fi_attachment.Name;
while (!fileSaved)
{
string file_ftpURI = string.Format("{0}/{1}", ftpURI, filename);
FtpWebRequest file_exist_request = (FtpWebRequest)FtpWebRequest.Create(file_ftpURI);
file_exist_request.Credentials = new NetworkCredential(ftp_user, ftp_pass);
file_exist_request.Method = WebRequestMethods.Ftp.GetFileSize;
try
{
FtpWebResponse response = (FtpWebResponse)file_exist_request.GetResponse();
}
catch (WebException ex)
{
FtpWebResponse response = (FtpWebResponse)ex.Response;
if (response.StatusCode ==
FtpStatusCode.ActionNotTakenFileUnavailable)
{
FtpWebRequest upload_request = (FtpWebRequest)FtpWebRequest.Create(file_ftpURI);
upload_request.Credentials = new NetworkCredential(ftp_user, ftp_pass);
upload_request.Method = WebRequestMethods.Ftp.UploadFile;
upload_request.UsePassive = true;
upload_request.UseBinary = true;
upload_request.KeepAlive = false;
StreamReader attachment = new StreamReader(fi_attachment.FullName);
byte[] attachmentData = Encoding.UTF8.GetBytes(attachment.ReadToEnd());
upload_request.ContentLength = attachmentData.Length;
//Stream upload_request_stream = upload_request.GetRequestStream();
using (Stream upload_request_stream = upload_request.GetRequestStream())
{
upload_request_stream.Write(attachmentData, 0, attachmentData.Length);
upload_request_stream.Close();
}
FtpWebResponse upload_response = (FtpWebResponse)upload_request.GetResponse();
fileSaved = true;
}
}
}
return fileSaved;
}
任何帮助解决这个问题都会很棒。
答案 0 :(得分:7)
您需要以二进制模式复制文件的内容,如下所示:
var response = (FtpWebResponse)ex.Response;
if (response.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable) {
FtpWebRequest upload_request = (FtpWebRequest)FtpWebRequest.Create(file_ftpURI);
upload_request.Credentials = new NetworkCredential(ftp_user, ftp_pass);
upload_request.Method = WebRequestMethods.Ftp.UploadFile;
upload_request.UsePassive = true;
upload_request.UseBinary = true;
upload_request.KeepAlive = false;
var attachment = File.Open(fi_attachment.FullName, FileMode.Open);
using (Stream upload_request_stream = upload_request.GetRequestStream()) {
attachment.CopyTo(upload_request_stream);
upload_request_stream.Close();
}
var upload_response = (FtpWebResponse)upload_request.GetResponse();
fileSaved = true;
}
您当前的程序将其作为UTF-8编码中的一个非常长的字符串读取,这可能是文件大小变化的原因。
答案 1 :(得分:2)
为什么所有这些舞蹈解码文件然后重新编码?你有两个流吗?您希望服务器上存在磁盘上的相同文件吗? Stream.CopyTo
会更容易出错。
你可以替换
StreamReader attachment = new StreamReader(fi_attachment.FullName);
byte[] attachmentData = Encoding.UTF8.GetBytes(attachment.ReadToEnd());
upload_request.ContentLength = attachmentData.Length;
//Stream upload_request_stream = upload_request.GetRequestStream();
using (Stream upload_request_stream = upload_request.GetRequestStream())
{
upload_request_stream.Write(attachmentData, 0, attachmentData.Length);
upload_request_stream.Close();
}
与(在我的头顶)
using (var fs = File.OpenRead(fi_attachment.FullName))
using (Stream upload_request_stream = upload_request.GetRequestStream())
{
fs.CopyTo(upload_request_stream);
}