0

I'm trying to make a module that downloads all of the files of an FTP server's directory and saves it as a .zip file to the user's Desktop. For the specific server I'm using, which I can't disclose its URL and credentials due to it being classified information, I'm getting an error saying the first file can't be read due to access denial:

WinSCP.SessionRemoteException: Error transferring file '/site/wwwroot/.dockerignore'.
Can't open file 'D:\.dockerignore'.
System Error.  Code: 5.
Access is denied
Copying files from remote side failed.

I'm not sure if it is this particular server that doesn't allow remote connections from unauthorized external sources or if there is some way of circumventing this using WinSCP.

HTML on .aspx page:

<asp:Label ID="Label116" runat="server" Text="Enter FTP Server Host Name (like &quot;ftp.example.com&quot; if &quot;ftp://ftp.example.com/directory/to/download&quot;): "></asp:Label>
<asp:TextBox ID="TextBox13" runat="server" class="text-box2" Width="668px" Height="38px"></asp:TextBox>
<br />
Enter Directory of FTP Host Name (like "directory/to/download" if "ftp://ftp.example.com/directory/to/download"):&nbsp;
<asp:TextBox ID="TextBox58" runat="server" class="text-box2" Width="400px" Height="38px"></asp:TextBox><br />
<asp:Label ID="Label117" runat="server" Text="Enter Username: "></asp:Label>
<asp:TextBox ID="TextBox56" runat="server" class="text-box2" Width="250px" Height="38px"></asp:TextBox>
<br />
<asp:Label ID="Label118" runat="server" Text="Enter Password: "></asp:Label>
<asp:TextBox ID="TextBox57" runat="server" class="text-box2" Width="250px" Height="38px" TextMode="Password"></asp:TextBox>
<asp:Button ID="Button26" runat="server" class="myButton" OnClick="Button26_Click" Text="Download Entire Repository of Files" />

C#:

protected void Button26_Click(object sender, EventArgs e)
{
    string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
    path = path.Replace(@"\", @"\\");
    SessionOptions sessionOptions = new SessionOptions
    {
       Protocol = Protocol.Ftp,
       HostName = TextBox13.Text,
       UserName = TextBox56.Text,
       Password = TextBox57.Text,
    };

    using (Session session = new Session())
    {
       // Connect
       session.Open(sessionOptions);

       // Download files    
       session.GetFiles("/" + TextBox58.Text + "/*", path + @"\*").Check();
    }
}

If you want to find this actual module I made on my website, you can find it on https://maxstechandmathsite.azurewebsites.net/Random about halfway down. Any help would be appreciated!

Max Voisard
  • 1,685
  • 1
  • 8
  • 18
  • It's a local error. Nothing to do with your server. You do not seem to have a write access to D:\ root; or the `D:\.dockerignore` file already exists, but you do not have a write permissions to it. – Martin Prikryl May 20 '19 at 10:37
  • Btw, this makes no sense to me: `path = path.Replace(@"\", @"\\");` -- But it's most likely not related to your immediate problem. – Martin Prikryl May 20 '19 at 10:38
  • The line you selected is to accomodate for escape characters (like \n for new line) in C#. Are you sure if it is write permissions and not read permissions I need to get these files, since I'm trying to download them and not write anything? By the way, this is the FTP server for my website, which holds my repository of files, so I would think my own username and password would have permissions. – Max Voisard May 20 '19 at 10:41
  • I understand that you **think** the line does -- But it does not do that. Escape characters are for string **literals** (which there are none in your code), not for values. – Martin Prikryl May 20 '19 at 10:43
  • I believe that the error clearly says *"Can't open file **'D:\.dockerignore'**."* -- That's a local file. -- Nothing to do with username and passwords to your **FTP server**. -- How are you running your code? --- What if you do `File.WriteAllText(@"D:\.dockerignore", "blah");`? – Martin Prikryl May 20 '19 at 10:45
  • But I'm still assuming that the path to store the files isn't the problem? If you think the D: drive is a local hard disk, it isn't; it's actually the drive to my FTP server. – Max Voisard May 20 '19 at 10:49
  • So if the D: is mapped to your FTP server, when your code is effectively downloading a file from FTP server (using WinSCP .NET assembly) and uploading it back to the (same?) FTP server (using mapped drive). --- WHY? – Martin Prikryl May 20 '19 at 10:57
  • You wrote that you wanted to *"save it as a .zip file to the user's Desktop"*. --- Plus, if you have your FTP mapped as a drive, why do you even use FTP protocol in your code? Why don't you just work with the files on the D: drive as with any other local files? (as they effectively are). – Martin Prikryl May 20 '19 at 10:58
  • lol I wish I gave you more information, you're misunderstanding the purpose of this code. I'm not trying to do a complex way of downloading my own site's files to the user's Desktop via FTP, what I'm REALLY trying to do is have an useful portal for users to download files from FTP servers, much like an FTP client like FileZilla, and I'm using my website's FTP server as a TEST server, since I know its credentials. Make sense now? – Max Voisard May 20 '19 at 11:05
  • OK, so I assume that you **think** that your code is downloading the files to user's local machine, while **actually** your code is downloading the files to your webserver. – Martin Prikryl May 20 '19 at 11:08
  • So you're saying it's downloading the files from the FTP server to my Azure web server? Ahhhh, I see now...I didn't configure the code to download a .zip file to the user's Desktop yet...but I still don't think that's the problem. If you can figure out this WinSCP access denial error (which I think its the read permissions), feel free to post an answer on it as I have to get going to work now...I can chat with you again in the afternoon and hopefully we'll have it figured out by then. Try testing FTP servers you know the credentials to on my website and see if they work. – Max Voisard May 20 '19 at 11:17
  • IT IS **THE PROBLEM**. There's no way your code can be modified to do what you want. – Martin Prikryl May 20 '19 at 11:21
  • OK, and if the first solution you had to that past question would also download to the web server and not a local machine, I'm out of luck. Is that the case? – Max Voisard May 20 '19 at 11:24
  • The other question is about downloading files from FTP server to **the machine, where the code is running** (that question has nothing to do with webservers). Your code is running **on the web server**. --- Your are on a completely wrong track. -- Delete your code and start form the scratch along this lines: [C# WebClient - downloading a file from an ftp to client](https://stackoverflow.com/q/33048958/850848). – Martin Prikryl May 20 '19 at 11:26
  • This maybe a better one: [Download file from FTP and how prompt user to save/open file in ASP.NET C#](https://stackoverflow.com/q/11047879/850848). – Martin Prikryl May 20 '19 at 11:27
  • Also see [How to upload and download files from a remote server in ASP.NET](https://support.microsoft.com/en-us/help/2512241/how-to-upload-and-download-files-from-a-remote-server-in-asp-net) – Martin Prikryl May 20 '19 at 11:28
  • Thanks, I'll look at in the afternoon – Max Voisard May 20 '19 at 11:29
  • Understand, that a web server cannot access file system of a local machine. That would be a terrible security nightmare. It would mean that any website you visit would be able to completely delete your local disk. Or even worse, install a malware. – Martin Prikryl May 20 '19 at 11:33

0 Answers0