简体   繁体   中英

FTP - Uploading a file in a diffrent name

I have a form which has radiobuttons, each of them is giving a file name in a string, and what I want to do is to have that string as a name for any file that a user uploads.

It'll be great if you could explain to me how to rename, because I already got it the code to upload or just help me modify this function, I would probably have to add to the parameters "string type" tho:

public void uploadFTP(string filename, string type, string password, ProgressBar progressbar)
{
    WebClient client = new WebClient();
    client.Credentials = new System.Net.NetworkCredential("username", password);

    try
    {
        client.UploadFileAsync(new Uri(@"ftp://ftpaddress.com" + "/" + new FileInfo(filename).Name), "STOR", filename);
    }
    catch(System.InvalidCastException)
    {
        // Handled it
    }
    catch (System.ArgumentException)
    {
        // Handled it
    }
    client.UploadProgressChanged += (s, e) =>
    {
        progressbar.Value = e.ProgressPercentage;
    };
    client.UploadFileCompleted += (s, e) =>
    {
        MessageBox.Show("Upload complete", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
    };
}

If it's important: The files are RTF (RichTextBox).

Thanks!

Just upload to different URL then. Replace new FileInfo(filename).Name in your code with the name you actually want. Also, I think not using string manipulation is better. And the STOR command is the default.

client.UploadFileAsync(new Uri(new Uri("ftp://ftpaddress.com"), newName)), filename);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM