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

发布于 2024-07-13 09:49:12 字数 1330 浏览 2 评论 0原文

这应该非常简单,并且可以上传。 但是当我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

娇女薄笑 2024-07-20 09:49:12

尽量不要使用 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 before puting a text file. Same principle likely applies here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文