Posting files

  • Thread starter Thread starter dana lees
  • Start date Start date
D

dana lees

Hi,

I am writing a C# asp.net application.

How can i post files to a web page? Can you please show an example?

I have 3 input files:

for example - <input type="file" size="40" id="file1" runat="server">

and a "load" button:

<asp:imagebutton id="btnLoad" Runat="server"
ImageUrl="../images/btnLoad.gif"></asp:imagebutton>

I would like to load 3 files and post them.

How do i do that?

Thanks,
Dana
 
Hi,
I am writing a C# asp.net application.

How can i post files to a web page? Can you please show an example?

I have 3 input files:

for example - <input type="file" size="40" id="file1" runat="server">

and a "load" button:

<asp:imagebutton id="btnLoad" Runat="server"
ImageUrl="../images/btnLoad.gif"></asp:imagebutton>

I would like to load 3 files and post them.

How do i do that?

Thanks,
Dana

see
http://msdn.microsoft.com/library/d...mwebuihtmlcontrolshtmlinputfileclasstopic.asp


Hans Kesting
 
Hi Dana,

when you make the <input type="file"....../> HTML control runat server it
becomes a HtmlInputFile control, which has a PostedFile property (which is
a HttpPostedFile object) that contains the uploaded file
you can simply save the file to a destination on your server by calling the
SaveAs(string fileName) method of the HttpPostedFile object. thus:

string fileName=file1.PostedFile.FileName;

FileInfo fileInfo = new FileInfo(fileName);

string newFileName=
string.Concat(Server.MapPath("UploadedFiles"),"/",fName,fileInfo.Extension);

file1.PostedFile.SaveAs(newFileName);



Or you can manipulate the stream object associated with the HttpPostedFile
object if you do not just want to save it. by :



HttpPostedFile post = file1.PostedFile;

byte[] arr = new byte[post.InputStream.Length];

post.InputStream.Read(arr,0,arr.Length);

// your file contents are now int the byte array arr
 
How do i do that?

In addition to the other replies, you need to bear in mind that the default
user under which ASP.NET apps run will almost certainly NOT have write
permission on the server, so you will need to rectify that either by
granting the default user additional permissions or by having your app
impersonate another user which does have those permissions.

Also, if your app is to be hosted on the public internet, make absolutely
certain that your ISP will allow you to do this - not all do...
 
I don't need to save those files to the server.
I need to post them to another web page.

I know i should cunstruct string similar to the following:

:-----------------------------7d610921440afa Content-Disposition: form-data;
name="req" AddCookieFiles -----------------------------7d610921440afa
Content-Disposition: form-data; name="site_id"
56550 -----------------------------7d610921440afa Content-Disposition:
form-data; name="sitename"
danaarie -----------------------------7d610921440afa Content-Disposition:
form-data; name="siteurl"
http://dsfsdfs -----------------------------7d610921440afa
Content-Disposition: form-data; name="file1"; filename="C:\Documents and
Settings\danal\Desktop\AEG.xls" Content-Type: application/vnd.ms-excel

I have done that in classic ASP before i don't know how to do that with
asp.net
 
if you are trying to post to another webserver from your asp.net server
code, then look at the webclient class.

-- bruce (sqlwork.com)
 
Back
Top