p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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.

Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old December 23rd, 2003, 02:55 AM
Registered User
Points: 13, Level: 1
Points: 13, Level: 1 Points: 13, Level: 1 Points: 13, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2003
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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 :)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #2 (permalink)  
Old December 23rd, 2003, 03:31 AM
Friend of Wrox
Points: 3,152, Level: 23
Points: 3,152, Level: 23 Points: 3,152, Level: 23 Points: 3,152, Level: 23
Activity: 20%
Activity: 20% Activity: 20% Activity: 20%
 
Join Date: Jun 2003
Location: Tehran, Iran
Posts: 922
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via Yahoo to melvik
Default

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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #3 (permalink)  
Old December 23rd, 2003, 04:30 AM
Registered User
Points: 13, Level: 1
Points: 13, Level: 1 Points: 13, Level: 1 Points: 13, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2003
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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 :)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #4 (permalink)  
Old December 23rd, 2003, 07:31 AM
Friend of Wrox
Points: 3,152, Level: 23
Points: 3,152, Level: 23 Points: 3,152, Level: 23 Points: 3,152, Level: 23
Activity: 20%
Activity: 20% Activity: 20% Activity: 20%
 
Join Date: Jun 2003
Location: Tehran, Iran
Posts: 922
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via Yahoo to melvik
Default

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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #5 (permalink)  
Old December 23rd, 2003, 09:37 PM
Registered User
Points: 13, Level: 1
Points: 13, Level: 1 Points: 13, Level: 1 Points: 13, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2003
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thanks again melvik..

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #6 (permalink)  
Old December 29th, 2003, 11:57 PM
planoie's Avatar
Friend of Wrox
Points: 16,368, Level: 55
Points: 16,368, Level: 55 Points: 16,368, Level: 55 Points: 16,368, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,394
Thanks: 0
Thanked 2 Times in 2 Posts
Default

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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Filter File Types in ASP.NET File Upload ramuis78 ASP.NET 2.0 Basics 2 May 31st, 2007 11:50 AM
file upload asudhakar C# 1 April 4th, 2007 08:29 AM
Whole Folder upload(Multi file Upload) ramasamy_rams XML 1 September 9th, 2005 01:43 PM
JSP file upload and delete file pandjie JSP Basics 0 January 29th, 2005 10:49 PM
Upload file hbcontract XML 2 November 6th, 2003 07:54 PM



All times are GMT -4. The time now is 02:19 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
© 2008 Wiley Publishing, Inc