After "File Field" is on Webform2.aspx, how do I upload it?

  • Thread starter Thread starter Trint Smith
  • Start date Start date
T

Trint Smith

I have a file field on Webform2.aspx and it works (as far as browsing
and getting file and path). What do I do next to get the file to upload?
Thanks,
Trint

..Net programmer
(e-mail address removed)
 
Your FORM tag must have the enctype=multipart/form-data attribute so it
looks something like this

<form method=post action=webform2.aspx runat=server
ENCTYPE=MULTIPART/FORM-DATA>

Your Input type=file tag also needs to have the runat=server attached to it
so it looks like this
<input type="file" id="oFile" runat="server">

then in your upload button you can save the file with something like this

request.files(0).SaveAs(server.mappath("filename.txt"))

Don't forget your server folder needs to be set to allow writes by the
ASPNet user.
 
Thanks,
How do I get the ("filename.txt"), to be either a .gif or .jpg and keep
it from beeing overwritten by the next aspnet user?
Trint


.Net programmer
(e-mail address removed)
 
well that's up to your code logic :). perharps you can generate a random net
by using GUIDs or CryptGenBytes (can't remember the correct name offhand)
 
I have made all the changes...now I get a Server Error page that says
"System.ArgumentException: Invalid path for MapPath
'http://trinity/bid1'. A virtual path is expected"...I get it when I
include the file.txt in the path also.
Thanks,
Trint

..Net programmer
(e-mail address removed)
 
I figured it out:
Request.Files(0).SaveAs(Server.MapPath("/WebApplication1/uploadfiles/new
file.gif"))
This works, but I would really like to grab the real name of the
file...is that possible? How?
Thanks,
Trint

.Net programmer
(e-mail address removed)
 
Use your MSDN library Trint, under help->Index of VIsual Studio.Net there's
an abundance of information there. :)

Anyway request.files(0).filename will get you the full CLIENT SIDE filename
of the file. Therefore if the user uploaded the file "c:\my
documents\file.txt" that's what you'll get.

Then you can use the System.IO.Path library to get the file.txt bit by using
System.IO.Path.GetFileName(request.files(0).filename)

So your save as code would now look lik
Request.Files(0).SaveAs(Server.MapPath("/WebApplication1/Uploadfiles/" &
System.IO.Path.GetFileName(request.files(0).filename) ))

By the way if you use
Server.Mappath("/afolder")
You;ll get a path that's relative to your IIS ROOT. so in the above case
you'd get a path c:\inetpub\wwwroot\afolder

But if you use
Server.Mappath("afolder")
It'll be relative to your executing file so you'll get something like
c:\inetpub\wwwroot\webapplication1\afolder

And use the ~ if you want the path to be relative to your app root :-
server.mappath("~\test\afolder")
Will return c:\inetpub\wwwroot\webapplication1\test\afolder
Even if your aspx file is in
c:\inetpub\wwwroot\webapplication1\someotherfolder.
 
Thanks,
One more thing...while it uploads, is there a way to show the upload
status?
Trint

.Net programmer
(e-mail address removed)
 
Back
Top