ASP.NET C#通过FTPwebRequest上传MemoryStream内容问题
这应该非常简单,并且可以上传。 但是当我在 FTP 服务器上打开上传的文件时,它显示二进制数据,这只是一些奇怪的字符,看起来像这样 [][][][],并且它的文件大小正确。 如何添加表明该文件是 XML 的属性或标头?
public bool ProcessBatch(MemoryStream memStream)
{
bool result = true;
FTPaddress = DistributionResources.ftpServer;
CompleteFTPPath = DistributionResources.ftpPath;
request = (FtpWebRequest)FtpWebRequest.Create(FTPaddress + CompleteFTPPath);
request.Credentials = new NetworkCredential("username", "password");
request.Method = WebRequestMethods.Ftp.UploadFile;
request.UsePassive = true;
request.UseBinary = true;
request.KeepAlive = false;
try
{
byte[] buffer = new byte[memStream.Length];
memStream.Read(buffer, 0, buffer.Length);
memStream.Close();
using (Stream reqStream = request.GetRequestStream())
{
reqStream.Write(buffer, 0, buffer.Length);
}
//Gets the FtpWebResponse of the uploading operation
response = (FtpWebResponse)request.GetResponse();
Console.WriteLine(response.StatusDescription); //Display response
}
catch(Exception ex)
{
result = false;
}
return result;
}
非常感谢
This should be pretty straight forward, and uploading works. BUT when I open the uploaded file on the FTP server it shows binary data which is just some weird characters that look like this [][][][], and its the right file size.
how do I add attributes or headers that that will say that this file is an XML?
public bool ProcessBatch(MemoryStream memStream)
{
bool result = true;
FTPaddress = DistributionResources.ftpServer;
CompleteFTPPath = DistributionResources.ftpPath;
request = (FtpWebRequest)FtpWebRequest.Create(FTPaddress + CompleteFTPPath);
request.Credentials = new NetworkCredential("username", "password");
request.Method = WebRequestMethods.Ftp.UploadFile;
request.UsePassive = true;
request.UseBinary = true;
request.KeepAlive = false;
try
{
byte[] buffer = new byte[memStream.Length];
memStream.Read(buffer, 0, buffer.Length);
memStream.Close();
using (Stream reqStream = request.GetRequestStream())
{
reqStream.Write(buffer, 0, buffer.Length);
}
//Gets the FtpWebResponse of the uploading operation
response = (FtpWebResponse)request.GetResponse();
Console.WriteLine(response.StatusDescription); //Display response
}
catch(Exception ex)
{
result = false;
}
return result;
}
Thank you very much
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尽量不要使用
request.UseBinary = true
换句话说,使用
request.UseBinary = false
。 否则,它会将内容作为二进制数据上传,这可能就是您看到它在服务器上以这种方式显示的原因。例如,如果您在 Windows 中使用命令行 FTP 客户端,则必须在
放入
文本文件之前显式键入ascii
。 同样的原则可能适用于此。Try not using
request.UseBinary = true
In other words, use
request.UseBinary = false
. Otherwise it will upload the contents as binary data, which is likely why you are seeing it show up that way on the server.For example, if you use the command line FTP client in windows, you have to explicitly type
ascii
beforeput
ing a text file. Same principle likely applies here.