ASP.NET C# upload MemoryStream content via FTPwebRequest issue

末鹿安然 提交于 2019-12-11 03:48:21

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/542709/asp-net-c-sharp-upload-memorystream-content-via-ftpwebrequest-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!