473,396 Members | 1,714 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

simulating input type = file from c# windows applicaiton

hi,

I can't seem to find the right way to simulate a file form post to a
web page from a window applicaiton and it seems like there should be
an easy way. I can post form variable like this:

string strId = "id";
string strName = "name";

ASCIIEncoding encoding=new ASCIIEncoding();
string postData="p_word="+strId;
postData += ("&u_name="+strName);
byte[] data = encoding.GetBytes(postData);

// Prepare web request...
HttpWebRequest myRequest =
(HttpWebRequest)WebRequest.Create("http://localhost/test.php");
myRequest.CookieContainer = new System.Net.CookieContainer(10000);
myRequest.Method = "POST";
myRequest.ContentType="application/x-www-form-urlencoded";
myRequest.ContentLength = data.Length;
Stream newStream=myRequest.GetRequestStream();
// Send the data.
newStream.Write(data,0,data.Length);
newStream.Close();
WebResponse res = myRequest.GetResponse();
StreamReader sr = new StreamReader( res.GetResponseStream());
string s = sr.ReadToEnd();

Debug.WriteLine(s);

What do I need to do in order to simulate a input type = file field?

thanks in advance,
Mike
Nov 16 '05 #1
2 4337
Mike,

You might want to try the WebClient class, and call the UploadFile
method. It should do what you are looking for.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"madmike" <ma*****@gmail.com> wrote in message
news:83**************************@posting.google.c om...
hi,

I can't seem to find the right way to simulate a file form post to a
web page from a window applicaiton and it seems like there should be
an easy way. I can post form variable like this:

string strId = "id";
string strName = "name";

ASCIIEncoding encoding=new ASCIIEncoding();
string postData="p_word="+strId;
postData += ("&u_name="+strName);
byte[] data = encoding.GetBytes(postData);

// Prepare web request...
HttpWebRequest myRequest =
(HttpWebRequest)WebRequest.Create("http://localhost/test.php");
myRequest.CookieContainer = new System.Net.CookieContainer(10000);
myRequest.Method = "POST";
myRequest.ContentType="application/x-www-form-urlencoded";
myRequest.ContentLength = data.Length;
Stream newStream=myRequest.GetRequestStream();
// Send the data.
newStream.Write(data,0,data.Length);
newStream.Close();
WebResponse res = myRequest.GetResponse();
StreamReader sr = new StreamReader( res.GetResponseStream());
string s = sr.ReadToEnd();

Debug.WriteLine(s);

What do I need to do in order to simulate a input type = file field?

thanks in advance,
Mike

Nov 16 '05 #2
Nicholas,

I looked at WebClient.FileUpload before and decided not to use it
because I knew I needed to set the cookie container as well as post
other form variables with the file. Am I wrong is assuming that
WebClient.FileUpload will not be able to do those things?

thanks,
Mike

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Barb | last post by:
I'm simulating keyboard input in Visual C++ using the SendInput function. It seems to work fine for all characters (A-Z, 0-9). I am however having real problems trying to read other characters such...
1
by: balu | last post by:
Hi, I am developing a standalone or windows based application in vb.net which simulates a website. I would like to simulate this web form from my vb.net application: <form action...
1
by: champ.supernova | last post by:
Hi, I have a form which has a few different submit links, all of type image. This is critcal to the operation of the form, as it relies on the image co-ords being passed to another page, the...
15
by: Nathan | last post by:
I have an aspx page with a data grid, some textboxes, and an update button. This page also has one html input element with type=file (not inside the data grid and runat=server). The update...
1
by: david sargent | last post by:
Ok, so I have read posts that say Firefox can't simulate a click on a <input type="file>, but I remember seeing an example for a file upload where the input file was in a hidden iFrame and it...
3
by: Wojtek | last post by:
Given the following somewhat abbreviated code: ------------------------------------------ <table> <form name="aForm1" action="doStuff" method="post"> <input type="hidden" name="id" value="1">...
15
by: Bjoern Schliessmann | last post by:
Hello all, I'm trying to simulate simple electric logic (asynchronous) circuits. By "simple" I mean that I only want to know if I have "current" or "no current" (it's quite digital) and the only...
0
by: Question123 | last post by:
Hi I have created one windows dekstop applicaiton with C#. that will accept cmd aruments. i have added this applicaiton in Windows Schedular task from where i am trying to pass aruments but it's...
1
by: paragbhavsar | last post by:
Hello Friends, Can we simulate click event of Input File Control through Javascript - I want this for Firefox 3.5 browser ? I have also tried with following code. Please let me know solution...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.