Wrox Programmer Forums
|
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 Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 23rd, 2003, 02:55 AM
Registered User
 
Join Date: Dec 2003
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 :)
 
Old December 23rd, 2003, 03:31 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 996
Thanks: 2
Thanked 11 Times in 11 Posts
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.
 
Old December 23rd, 2003, 04:30 AM
Registered User
 
Join Date: Dec 2003
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 :)
 
Old December 23rd, 2003, 07:31 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 996
Thanks: 2
Thanked 11 Times in 11 Posts
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.
 
Old December 23rd, 2003, 09:37 PM
Registered User
 
Join Date: Dec 2003
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thanks again melvik..

 
Old December 29th, 2003, 11:57 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 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.





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 10:50 AM
file upload asudhakar C# 1 April 4th, 2007 07:29 AM
Whole Folder upload(Multi file Upload) ramasamy_rams XML 1 September 9th, 2005 12: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





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.