Click here to Skip to main content
15,903,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all, just experimenting with uploading a file to my local web server in preparation for a task I have and I'm having problems uploading a very small file - no errors it just doesn't arrive at the server, the directory permissions on the server ( I changed them whilst troubleshooting ) are read / write for the whole world ( it's an Apache server ) so I'm asking for help - downloading files works perfectly.
Here is what I've tried

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace DownloadFile
    class DownloadFile
        WebClient client;

        static void Main(string[] args)
            String URLRoot = @"";
            String UploadFname = "File.txt";
            String Msg = "This is headed for the server";


            String UploadFullPath = Path.Combine(URLRoot,UploadFname);

            DownloadFile df = new DownloadFile();
            df.client = new WebClient();
            if (File.Exists(UploadFname))
                 df.client.UploadFile(UploadFullPath, UploadFname);

What I have tried:

As shown in the code above
Updated 24-Feb-16 2:30am
Kornfeld Eliyahu Peter 24-Feb-16 7:22am    
What have to be 30 letters?
pkfox 24-Feb-16 7:23am    
The question and what have you tried :-)
Kornfeld Eliyahu Peter 24-Feb-16 7:49am    
Oh! Ha!
I didn't got it how 30 connected to all your code...
Do you have a HTTP request log there? Maybe there is an error in it?
Why do you complicate things with that DownloadFile.client combination instead of declaring a local WebClient?
Try new Uri(new Uri(@""), UploadFname) instead of Path.Combine...
pkfox 24-Feb-16 8:29am    
new Uri(new Uri(@""), UploadFname) don't know what you mean by that ? , you have two uri's in there

Edit - oh I see it creates a new URi - I've tried it though ( and got rid of the class complication ) and still no joy - and still no errors
Kornfeld Eliyahu Peter 24-Feb-16 8:37am    
Path.Combine will handle your strings like local paths, with \...
Uri will combine them as...URI, with /

Uri base = new Uri(@"");
Uri taget = new Uri(base, UploadFname);

1 solution

You misunderstand what UploadFile does. It doesn't save the file to the target server, it simply POSTs the file to the url as if it had been a form submission;

<form action="upload.aspx">
    <input type="file" name="myfile"/>

So the url you "upload" to has to be a page that can read the files posted to it and save them.

WebClient.UploadFile Method (String, String) (System.Net)[^]
Share this answer
pkfox 24-Feb-16 8:54am    
Ah ! that would explain it thanks ( I've not done much Web stuff ) so what would be the way to upload a file ? ( there is no ftp server on the box )
F-ES Sitecore 24-Feb-16 8:55am    
Write an upload page in php that you can post to and have that page save the file.
pkfox 24-Feb-16 9:27am    
Ok thanks that's beyond me at the moment as I've never done any Web stuff

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900