amateur programmer
amateur programmer

Reputation: 331

Uploading a file to server using c#

I am trying to upload a file to my server and after that I will be saving the path in my database but the file upload isn't working, It doesn't make an exception but when I check my folder I don't see any files uploaded here is my code:

private void bBrowse_Click(object sender, EventArgs e)
    {
        long size = -1;
        string path = "";
        openFileDialog1.FileName = "";
        openFileDialog1.Filter = "All files (*.*)|*.*|All files (*.*)|*.*"; 
        DialogResult result = openFileDialog1.ShowDialog(); // Show the dialog.
        if (result == DialogResult.OK) // Test result.
        {

            openFileDialog1.Title = "Attach speech assessment document";
            openFileDialog1.Filter = "Doc Files|*.doc|Docx File|*.docx|PDF doc|*.pdf";
            openFileDialog1.InitialDirectory = @"C:\";
            fileName = System.IO.Path.GetFileName(openFileDialog1.FileName);
            path = Path.GetDirectoryName(openFileDialog1.FileName);
            labelFileName.Text = path + "/" + fileName;


        }
        Console.WriteLine(path); // <-- Shows file path in debugging mode.

        Console.WriteLine(result); // <-- For debugging use.
    }

    private void buttonAdd_Click(object sender, EventArgs e)
    {
        try
        {
            WebClient client = new WebClient();

            NetworkCredential nc = new NetworkCredential("username", "password");

            Uri addy = new Uri(@"http://url/public_html/assessment" + fileName);

            client.Credentials = nc;
            byte[] arrReturn = client.UploadFile(addy, labelFileName.Text);
            MessageBox.Show(arrReturn.ToString());

        }
        catch (Exception ex1)
        {
            MessageBox.Show(ex1.Message);
        }

Upvotes: 0

Views: 1616

Answers (1)

amateur programmer
amateur programmer

Reputation: 331

I solved it thanks to @William's comment, I forgot to add the slash and I also wrote http in the url instead of ftp that was the major mistake!

The code after the editing:

private void bBrowse_Click(object sender, EventArgs e)
    {
        long size = -1;
        string path = "";
        openFileDialog1.FileName = "";
        openFileDialog1.Filter = "All files (*.*)|*.*|All files (*.*)|*.*"; 
        DialogResult result = openFileDialog1.ShowDialog(); // Show the dialog.
        if (result == DialogResult.OK) // Test result.
        {

        openFileDialog1.Title = "Attach speech assessment document";
        openFileDialog1.Filter = "Doc Files|*.doc|Docx File|*.docx|PDF doc|*.pdf";
        openFileDialog1.InitialDirectory = @"C:\";
        fileName = System.IO.Path.GetFileName(openFileDialog1.FileName);
        path = Path.GetDirectoryName(openFileDialog1.FileName);
        labelFileName.Text = path + "/" + fileName;


    }
    Console.WriteLine(path); // <-- Shows file path in debugging mode.

    Console.WriteLine(result); // <-- For debugging use.
}

private void buttonAdd_Click(object sender, EventArgs e)
{
    try
    {
        WebClient client = new WebClient();

        NetworkCredential nc = new NetworkCredential("username", "password");

        Uri addy = new Uri(@"ftp://url/public_html/assessment/" + fileName);

        client.Credentials = nc;
        byte[] arrReturn = client.UploadFile(addy, labelFileName.Text);
        MessageBox.Show(arrReturn.ToString());

    }
    catch (Exception ex1)
    {
        MessageBox.Show(ex1.Message);
    }

Upvotes: 1

Related Questions