upload code info required!

D

Dam6

Hello there,

I'm using the below code and it works, but as usual, it does not quite give
me everything I need:

No matter what file I click on, it only ever save the file as test.txt and
that's my problem... This upload function will be used for various filetypes
and thus i need the code to take the filename of the file I am uploading,
or, somehow ask me what filename I would like!



Any ideas?



<%@ Page Language="C#" ContentType="text/html" ResponseEncoding="iso-8859-1"
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Upload</title>
<script runat="server">
void btnUploadFile_OnClick( Object o, EventArgs e )
{
fileUpload.PostedFile.SaveAs(Server.MapPath("test.txt") );
}
</script>
</head>
<body>
<form runat="server" enctype="multipart/form-data">
<input name="fileUpload" type="file" id="fileUpload" runat="server">
<asp:button ID="btnUploadFile" runat="server" Text="Upload File"
OnClick="btnUploadFile_OnClick" />
</form>
</body>
</html>
 
K

Karl Seguin

How can this not be expected?

You are telling it to save as test.txt....you have "test.txt" hardcoded in
there...

fileUpload.PostedFile.SaveAs(Server.MapPath("test.txt") );

Something like this should work:
string fileName =
System.IO.Path.GetFileName(fileUpload.PostedFile.FileName);
fileUpload.PostedFile.SaveAs("Server.MapPath(".") "\\" + fileName);

karl
--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
 
D

Dam6

Thank you for the reply!

I have now added your code as follows: (With the error when ran)

Compiler Error Message: CS1010: Newline in constant



<script runat="server">
void btnUploadFile_OnClick( Object o, EventArgs e )
{
string filename=
System.IO.Path.GetFileName(fileUpload.PostedFile.Filename);
fileUpload.PostedFile.SaveAs("Server.MapPath(".") "\\" + fileName);
}

</script>
</head>
<body>
<form runat="server" enctype="multipart/form-data">
<input name="fileUpload" type="file" id="fileUpload" runat="server">
<asp:button ID="btnUploadFile" runat="server" Text="Upload File"
OnClick="btnUploadFile_OnClick" />
</form>
</body>
</html>
 
K

Karl Seguin

System.IO.Path.GetFileName(fileUpload.PostedFile.Filename);
fileUpload.PostedFile.SaveAs(REMOVE_THE_DOUBLE_QUOTE_HERE
Server.MapPath(".") "\\" + fileName);

I put an extra " in the SaveAs

anywyas, if you play with the above code you ought to get it to work

If you get stuck again, check out this straightforward article:
http://www.4guysfromrolla.com/webtech/091201-1.shtml

Karl
--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,772
Messages
2,569,588
Members
45,100
Latest member
MelodeeFaj
Top