ASP.NET C#通过FTPwebRequest问题上传MemoryStream内容 [英] ASP.NET C# upload MemoryStream content via FTPwebRequest issue
问题描述
这应该很简单,并且可以上传.但是,当我在FTP服务器上打开上传的文件时,它显示的是二进制数据,它只是一些看起来像[[] [] [] []的怪异字符,以及正确的文件大小. 如何添加表示该文件是XML的属性或标头?
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;
}
非常感谢您
推荐答案
尝试不使用request.UseBinary = true
换句话说,使用request.UseBinary = false
.否则,它将以二进制数据的形式上载内容,这可能是为什么您看到它在服务器上以这种方式显示的原因.
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.
例如,如果在Windows中使用命令行FTP客户端,则必须在put
文本文件之前显式键入ascii
.同样的原则可能在这里适用.
For example, if you use the command line FTP client in windows, you have to explicitly type ascii
before put
ing a text file. Same principle likely applies here.
这篇关于ASP.NET C#通过FTPwebRequest问题上传MemoryStream内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!