Hello,
I am writing a C# command line application that uploads files to a website. Basically, imagine this form in a web application:
[HTML]<form method="post" enctype="mulipa rt/form-data" action="http://www.myfilestora ge.com/">
<input type="text" name="documentI d" value="123" />
<input type="file" name="document" value="C:\myfil e.doc" />
</form>[/HTML]
I need the same thing to happen in my command line application as what would happen in the web application when that form gets submitted. In other words, I have to make a request to a specific URL with two parameters: one text parameter and one file parameter. Does anyone know how to do this?
Here's what I have so far: - void UploadDocument(int documentId, byte[] documentBytes)
-
{
-
//create a new request
-
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.myfilestorage.com/");
-
request.Method = "POST";
-
request.ContentType = "multipart/form-data";
-
-
//write the parameters to the request
-
Stream requestStream = request.GetRequestStream();
-
/* ???? */
-
requestStream.Close();
-
-
//get the response to the request
-
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
-
response.Close();
-
}
1 4514
I've figured out a solution. Here it is: - void UploadDocument(int documentId, string fileName, byte[] fileBytes)
-
{
-
//create a new request
-
string boundary = "AaB03x";
-
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.myfilestorage.com/");
-
request.Method = "POST";
-
request.ContentType = "multipart/form-data; boundary=" + boundary + ";";
-
-
byte[] beforeFileBytes = Encoding.UTF8.GetBytes(
-
"--" + boundary + "\r\n" +
-
"Content-Disposition: form-data; name=\"documentId\"\r\n\r\n" +
-
documentId + "\r\n" +
-
"--" + boundary + "\r\n" +
-
"Content-Disposition: form-data; name=\"document\"; filename=\"" + fileName + "\"\r\n" +
-
"Content-Type: application/octet-stream\r\n" +
-
"Content-Transfer-Encoding: binary\r\n\r\n");
-
-
byte[] afterFileBytes = Encoding.UTF8.GetBytes("\r\n" +
-
"--" + boundary + "--" + "\r\n");
-
-
byte[] postData = new byte[beforeFileBytes.Length + fileBytes.Length + afterFileBytes.Length];
-
-
Stream stream = new MemoryStream(postData);
-
stream.Write(beforeFileBytes, 0, beforeFileBytes.Length);
-
stream.Write(fileBytes, 0, fileBytes.Length);
-
stream.Write(afterFileBytes, 0, afterFileBytes.Length);
-
stream.Close();
-
-
//write the post data to the request
-
request.ContentLength = postData.Length;
-
Stream requestStream = request.GetRequestStream();
-
this.WriteToStream(requestStream, postData);
-
requestStream.Close();
-
-
//get the response
-
WebResponse response = request.GetResponse();
-
response.Close();
-
}
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Bill Cohagan |
last post by:
I'm writing a console app (in C#) and I want to be able to redirect the
standard input/output streams when it's run at a command prompt. IOW I want
to support the "<" and ">" redirection syntax. The obvious way to do this is
by using the static Console type properties, In and Out. When trying to
debug the app in the IDE however, this doesn't appear to work. I've edited
the project properties and added the necessary text to the "command...
|
by: Fred Iannon |
last post by:
All,
I have developed an application that needs to run
in one of two modes:
(1) IF NO command line parms are provided I would
like it to run as a Window form application
(i.e. /target:winexe) with a Main form, etc. and with
execution returing to the command line AS SOON AS the
program STARTS
(2) If a command line parm IS provided I would
|
by: Oz |
last post by:
This is long. Bear with me, as I will really go through all the convoluted
stuff that shows there is a problem with streams (at least when used to
redirect stdout).
The basic idea is that my application (VB.NET) will start a process,
redirect its stdout and capture that process' output, displaying it in a
window.
I've written a component for this, and a test application for the component.
It allows me to specify a command to execute,...
|
by: mcl chan |
last post by:
Hello all,
I'm using below code for c# app to call another app
{
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc Enable.RaisingEvents=false;
proc StartInfo.Filename="anotherapp.exe" ;
proc StartInfor.Arguments="1234";
proc Start();
|
by: Peter Steele |
last post by:
I have a Windows application written in C# that I want to return a non-zero
exit code when it it run. The problem is that as a Windows application,
there doesn't seem to be a way to control this. The value returned by the
Main function has no impact on the value returned by the application.
Windows applications exit immediately, leaving their windows still open. Is
there a way to change the value a Windows application exits with. Basically...
| |
by: Al Cadalzo |
last post by:
I'm trying to simulate a form post (i.e. Method="POST").
The FORM POST I'm trying to simulate is similar to this:
<FORM NAME=SearchForm METHOD=POST ACTION=Search>
<SELECT name="criteriaA" >
<OPTION VALUE=1>AAAAAAAAAAA</OPTION>
<OPTION VALUE=2>BBBBBBBBBBBB</OPTION>
</SELECT>
....
|
by: Michael C |
last post by:
Is it possible to have an app that is both a console app and a windows app?
If there are no command line switches then it will run as a console app, if
there are command line switches then it will be a windows app. If I make it
a windows app then I can't write to the console. If I make it a console app
then it starts a console if the user runs it from the start menu so from
what I see it's not possible.
Thanks in advance,
Michael
|
by: Alex |
last post by:
Hello,
I'm wroting a console utility in VB 2005, and I need the ability to pass
parameters to the application when the program runs. For example, if my
program is called testing.exe, I need to be able to run this:
c:\testing.exe c:\test
....and have 'c:\test' passed to the program as a variable. Most of my VB
books focus on web forms or windows forms, but not much details on console
|
by: TuckerRoth |
last post by:
This is my first post on this site, so I don't know the rules. Please correct me if this gets to the wrong folks...etc.
PROBLEM: Many of the apps that I am writing need to be able to run 2 ways: They might be invoked from the command-line with parameters so that they do not show UI, but accomplish some task; OR they might be double-clicked, in which case they show a UI (so that the User can configure settings for the app...etc.).
To...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |