A
A
Andrew2021-12-19 01:17:04
ASP.NET
Andrew, 2021-12-19 01:17:04

How to upload a file via ftp if the host is Linux?

The site is on Asp.Net Core 3.1, people upload files to the site, these files need to be saved to FTP.
FtpWebRequest is used for downloading, Stream is created like this:

public Stream GetUploadStream(string filePath)
        {
            var request = (FtpWebRequest)WebRequest.Create(_ftp.Server + filePath);
            request.Credentials = _credential;
            request.EnableSsl = true;
            request.UseBinary = true;
            request.Method = WebRequestMethods.Ftp.UploadFile;
            
            return request.GetRequestStream();
        }

The download itself goes like this
// file - IFormFile из multipart form
            var requestStream = ftp.GetUploadStream(filePath);
            await file.OpenReadStream().CopyToAsync(requestStream);
            requestStream.Close();


This code works fine under Windows (when run in studio via iis express), but doesn't work when run on Linux.
Error when trying to upload file on linux
Message = The remote server returned an error: (550) File unavailable (eg, file not found, no access).

If you cast Exception to WebException and look at ((FtpWebResponse) e.Response).StatusDescription, then the error is
550 read tcp ip:port-\u003Eip:port: read: connection reset by peer\r\n

This creates a file of zero length.
Configs, ftp server, login and password are all the same.
I tried to send an array of bytes instead of a stream (more precisely, an array of characters from several characters), and this worked successfully. At the same time, converting the input file to an array of bytes does not help - the error is the same.
What could be the problem and how to solve it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Leonid, 2021-12-19
@caballero

FTP in general, it has nothing to do with it anyway.
Usually the difference is that on Linux filenames are case sensitive.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question