2
<form action="http://s0.filesonic.com/abc" method="post" enctype="multipart/form-data">
    <input type="file" name="files[]" />
    <button type="submit">submit</button>
</form>

The above code uploads the files to file sonic server, but I want to do this using programmatically using C#, basically my requirement is that the program creates the form and file control and sends the file to the Filesonic server URL mentioned in action attribute..

I have gone through many links but with no success, I have gone through the following links with no success.

Upload files with HTTPWebrequest (multipart/form-data)

4
  • This isn't entirely clear... Do you need a C# application that will connect to a server and upload a file? Or do you need your web page to upload a file automatically without user interaction? Commented Nov 16, 2011 at 16:35
  • I need my web page to upload a file without user interaction using c# Commented Nov 16, 2011 at 16:37
  • @david can you help me some code for that thing? Commented Nov 16, 2011 at 16:39
  • 1
    @ArunKumar: The client-side code can't be C#. You'll have to use JavaScript. How much automation needs to be done? I'm not sure if you can reliably upload a file with absolutely no user interaction. Browsers may require that the user be prompted to select the file, since for obvious security reasons web pages don't have much access to the host machine's file system. Commented Nov 16, 2011 at 16:43

3 Answers 3

2

The following code will upload the file to the server as long as the server can accept it outside of files[] array.

WebRequest webRequest = WebRequest.Create("http://s0.filesonic.com/abc");
FileStream reader = new FileStream("file_to_upload", FileMode.Open);

byte[] data = new byte[reader.Length];
webRequest.Method = "POST";
webRequest.ContentType = "multipart/form-data";
webRequest.ContentLength = reader.Length;
webRequest.AllowWriteStreamBuffering = "true";

reader.Read(data, 0, reader.Length);

using (var request = webRequest.GetRequestStream())
{
    request.Write(data, 0, data.Length);
    using (var response = webRequest.GetResponse())
    {
        //Do something with response if needed
    }
Sign up to request clarification or add additional context in comments.

1 Comment

@peter server will not accept the file unless it is in the file[] tag, that the problem
0

I that case your action on the form would point to your own page on your asp.net server. You are going to post a file back to your asp.net server using http, you will then either hold it in memory or write it to a temp directory, then you could HttpWebRequest to send the file to the filesonic server.

In your case you can also do form a post directly using HttpWebRequest, a quick sample that i could find is here

3 Comments

can you explain how httpclient can send the file to filesonic server using c# code
I tried your solution but it is not working I am getting the response from the server, but file is not uploaded to server
one other thing i might do in that case is download fiddler and watch the exact traffic going if you do it through form, and then watch it using the httpwebrequest
0

You can upload file to your server using FTP credentials Here , path means your local file path or source file & DestinationPath is server path where you have to upload file Ex. 'www.....com/upload/xxx.txt'

FtpWebRequest reqObj = (FtpWebRequest) WebRequest.Create(DestinationPath);                                   
reqObj.Method = WebRequestMethods.Ftp.UploadFile;                          
reqObj.Credentials = new NetworkCredential(FTP_USERNAME, FTP_PASSWORD);

byte[] fileContents = File.ReadAllBytes(path);   
reqObj.ContentLength = fileContents.Length;

Stream requestStream = reqObj.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();
FtpWebResponse response = (FtpWebResponse)reqObj.GetResponse();
response.Close();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.