|
|
 |
| ASP.NET 1.0 and 1.1 Basics ASP.NET discussion for users new to coding in ASP.NET 1.0 or 1.1. NOT for the older "classic" ASP 3 or the newer ASP.NET 2.0. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the ASP.NET 1.0 and 1.1 Basics section of the Wrox p2p Programmer to Programmer discussion community. This is a community of more than 40,000 computer programmers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining our free Wrox p2p community you can post your own programming questions and respond to other programmers’ questions. Registered users also don't have to see the ads that are displayed to guests. Registration is fast, simple and absolutely free so please, join today!
Join today and post to win prizes! Post more to increase your chances of being Wrox’s top poster of the month.
|
 |

December 23rd, 2003, 02:55 AM
|
|
Registered User
|
|
Join Date: Dec 2003
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
file upload
Hi,
I'm a newbie in ASP.Net
Right now I'm trying to create a open file dialog and using the below method :
<INPUT id="fileUpload" style="DISPLAY: none" type="file" onchange="txtFilename.value = this.value;">
I'm able to open the file and display the path name to the textbox 'txtFilename' using the above control...
But at the same time I would like to display out the contents of the file retrieved from the above path to another textbox..
Here's my code to read the contents of the file :
Dim srReadfile As StreamReader
'create a new streamReader from the path given in txtFilename text box
srReadfile = New StreamReader(txtFilename.Text)
'read the stream till the end of stream and display to txtQueryStmt text box
txtTextBox1.Text = srReadfile.ReadToEnd()
'close the streamReader
srReadfile.Close()
My questions is how do I display the path name and also the contents of the file at the same time when I click on the above fileUpload control ??
Is it I have to call a new routine/function on the 'onchange' events or other events ??
Or you all have any other better approaches/methods ?
Thanks in advance :)
|

December 23rd, 2003, 03:31 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Location: Tehran, Iran
Posts: 922
Thanks: 0
Thanked 1 Time in 1 Post
|
|
hope these code can help u!
Code:
<%@ Page debug="true" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>::: UPLOAD SAMPLE ::: </title>
<script language="c#" runat="server">
void Page_Load(Object sender, EventArgs e)
{
if (Page.IsPostBack)
SaveImages();
}
System.Boolean SaveImages()
{
System.Web.HttpFileCollection colFiles = System.Web.HttpContext.Current.Request.Files;
System.Text.StringBuilder strMessage;
strMessage = new System.Text.StringBuilder("Files Uploaded:<br><br>");
System.Int32 intFileCntr;
try
{
for (intFileCntr = 0; intFileCntr < colFiles.Count; intFileCntr++)
{
System.Web.HttpPostedFile objCurrentFile = colFiles.Get(intFileCntr);
System.String strCurrentFileName;
System.String strCurrentFileExtension;
strCurrentFileName = System.IO.Path.GetFileName(objCurrentFile.FileName);
if(strCurrentFileName != "")
{
strCurrentFileExtension = System.IO.Path.GetExtension(strCurrentFileName);
if (strCurrentFileExtension == ".gif" || strCurrentFileExtension == ".jpg")
{
objCurrentFile.SaveAs(Server.MapPath("books\\") + strCurrentFileName);
strMessage.Append(strCurrentFileName + " successfully uploaded.<BR>");
}
else
{
strMessage.Append(strCurrentFileName + " failed!! Only .gif and .jpg images allowed! <BR>");
}
}
}
Label1.Text = strMessage.ToString();
return true;
}
catch (System.Exception Ex)
{
Label1.Text = Ex.Message;
return false;
}
}
</script>
</HEAD>
<body>
<center>
<form id="UPLOAD" method="post" encType="multipart/form-data" runat="server">
<h3>Multiple File Upload Example</h3>
<P><INPUT id="File1" type="file" size="50" name="File1" runat="server"></P>
<P><INPUT id="File2" type="file" size="50" name="File2" runat="server"></P>
<P><INPUT id="File3" type="file" size="50" name="File3" runat="server"></P>
<P><INPUT id="File4" type="file" size="50" name="File4" runat="server"></P>
<P><INPUT id="File5" type="file" size="50" name="File5" runat="server"></P>
<P><STRONG>:: </STRONG>
<asp:linkbutton id="LinkButton1" runat="server" Font-Size="XX-Small" Font-Bold="True" Font-Names="Verdana">Upload Images</asp:linkbutton><STRONG>::
</STRONG><A id="LinkButton2" style="FONT-WEIGHT: bold; FONT-SIZE: xx-small; FONT-FAMILY: verdana" href="java script:document.forms[0].reset()">
Reset Form</A> <STRONG>::</STRONG></P>
<P><asp:label id="Label1" runat="server" Font-Size="XX-Small" Font-Bold="True" Font-Names="verdana" BorderColor="White" BorderStyle="None" Width="400px"></asp:label></P>
<P>
<asp:Label id=Label2 runat="server">Label</asp:Label></P>
<P>
<asp:Label id=Label3 runat="server">Label</asp:Label></P>
</form>
</center>
</body>
</HTML>
Always:),
Hovik Melkomian.
|

December 23rd, 2003, 04:30 AM
|
|
Registered User
|
|
Join Date: Dec 2003
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
hi melvik,
thanks...:)
but errr...i don't really understand the code
Currently I can display the contents of the file to another textbox using my following code :
Dim srReadfile As StreamReader
srReadfile = New StreamReader(txtFilename.Text)
txtTextBox1.Text = srReadfile.ReadToEnd()
srReadfile.Close()
Right now I have these 5 controls on my webpage :
<asp:textbox id="txtFilename" runat="server"></asp:textbox>
<asp:textbox id="txtContents" runat="server"></asp:textbox>
<INPUT id="fileUpload" style="DISPLAY: none" type="file" onchange="txtFilename.value = this.value;">
<input id="btnBrowse" onclick="fileUpload.click()" type="button" value="Browse" name="btnBrowse" runat="server">
<asp:button id="btnViewContent" runat="server" text="View Content"></asp:button>
the file open dialog and filename path can be displayed on the txtFilename textbox when i click on the btnBrowse button..
and the contents of the file(from the filename path of txtFilename) will be displayed to another textbox (ie. txtContents) when i click on the 2nd button, btnViewContent
but what I hope to achieve is :
I just need to click on 1 button, which is btnBrowse...
and then I will be able to get the file open dialog and choose the file I want..
and after I had chosen the file, the pathname will be displayed at 1 textbox (ie. txtFilename) and at the same time, the contents of the file I had chosen will be displayed at another textbox (ie. txtContents)
sorry for my poor explaination...
and really appreciate it if anyone can help..thanks in advance :)
|

December 23rd, 2003, 07:31 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Location: Tehran, Iran
Posts: 922
Thanks: 0
Thanked 1 Time in 1 Post
|
|
if u cant undersatand the code plz refer to Documenteation but its a sample to help u go on... thats all from me.
Always:),
Hovik Melkomian.
|

December 23rd, 2003, 09:37 PM
|
|
Registered User
|
|
Join Date: Dec 2003
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
thanks again melvik..
|

December 29th, 2003, 11:57 PM
|
 |
Friend of Wrox
Points: 16,368, Level: 55 |
|
|
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,394
Thanks: 0
Thanked 2 Times in 2 Posts
|
|
Here is the MSDN article that explains about the file input control. There is a sample there.
You'll have to put another control on the page to actual cause a postback of the page so you can then handle the file that was chosen.
There's something I need to point out about what you have first posted to offer clarification on the concept:
'----------------------------------------
'create a new streamReader from the path given in txtFilename text box
srReadfile = New StreamReader(txtFilename.Text)
'----------------------------------------
There's an inherent problem with what you are attempting here. When you use a file input control, you are providing the facility for the user to upload a file. Your web server doesn't have any access to that user's file system, the file is embedded as part of the request. The code you are using is attempting to read the file from the local file system (server). It just so happens that it works fine on your development system because the "two" machines (client and server) are one and the same. Try this from another machine and you'll have problems.
Take a look at the MSDN sample and you should be able to get it.
Peter
------------------------------------------------------
Work smarter, not harder.
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
 |