ASP.NET C#通过FTPwebRequest问题上传MemoryStream内容

时间:2022-05-21 05:48:47

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?

这应该是非常简单的,上传工作。但是当我在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;
    }

Thank you very much

非常感谢你

1 个解决方案

#1


Try not using request.UseBinary = true

尽量不要使用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.

换句话说,使用request.UseBinary = false。否则它会将内容上传为二进制数据,这可能就是为什么你看到它在服务器上以这种方式出现的原因。

For example, if you use the command line FTP client in windows, you have to explicitly type ascii before puting a text file. Same principle likely applies here.

例如,如果在Windows中使用命令行FTP客户端,则必须在放置文本文件之前显式键入ascii。同样的原则可能适用于此。

#1


Try not using request.UseBinary = true

尽量不要使用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.

换句话说,使用request.UseBinary = false。否则它会将内容上传为二进制数据,这可能就是为什么你看到它在服务器上以这种方式出现的原因。

For example, if you use the command line FTP client in windows, you have to explicitly type ascii before puting a text file. Same principle likely applies here.

例如,如果在Windows中使用命令行FTP客户端,则必须在放置文本文件之前显式键入ascii。同样的原则可能适用于此。