Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > ASP.NET 1.1
|
ASP.NET 1.1 As of 10/6/2005, this forum is locked as part of the reorganization described here: http://p2p.wrox.com/topic.asp?TOPIC_ID=35394. No posts have been deleted. Open ongoing discussions from the last week have been moved to either ASP.NET 1.0 and 1.1 Beginners http://p2p.wrox.com/asp-net-1-0-1-1-basics-60/ or ASP.NET 1.0 and 1.1 Professional. http://p2p.wrox.com/forum.asp?FORUM_ID=50. See my sticky post inside for more.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.1 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 May 22nd, 2004, 07:22 AM
Authorized User
 
Join Date: Nov 2003
Posts: 42
Thanks: 0
Thanked 0 Times in 0 Posts
Default File Upload in ASP.NET using HtmlInputFile control

Hi, I've created a file upload ASP.NET application using C#. I'm using a HtmlInputFile control to upload the file.

When I browse for a file and upload it to a new location, the upload works fine. The file is saved successfully to the new location. All permissions, encryption type are all fine.

The problem that I'm facing is, when I browse the file, rename it in the control and upload it to a new location, the upload still works fine, but is created as a new file. I mean a fresh new file is created with 0 bytes. Instead what I was expecting was that the browsed file be saved to the new location with the renamed file name and with all its content intact. But that is not happening.

I'm using the method: <filecontrol>.PostedFile.SaveAs(<path>).

I tried assigning the posted file to a new HttpPostedFile object, but that doesn't help too.

I will be thankful for any guidance given by the readers.

TIA
Praveen

 
Old May 23rd, 2004, 06:28 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

What I am understanding you are saying is that you change the name of the file on the page before you post it? As in, you select a file (from Browse...) and then you manually edit the file name in that input box? If this is the case, then this is the problem. If you change the filename how does the browser know what file to upload? It is basing the file it is sending to the server on that input box. If you change the name of the file and that file doesn't exist on the client machine, then what can it send? 0 Bytes.

The solution might be to provide a "Rename file:" input box so you can type the new name that the server will use to save the file (or save and rename if you have to).

Peter
-------------------------
Work smarter, not harder
 
Old May 23rd, 2004, 11:20 PM
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

This is a sample witch works fine, HTH.
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 June 30th, 2004, 06:45 AM
krp krp is offline
Registered User
 
Join Date: Jun 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi
I would like to be able to upload a file without a user needing to browse or type anything at the client. The client ASP.NET page knows the path of the file that is to be uploaded, but for some wierd reason there is no way to assign this to the value property of HtmlInputFile: it just can't be assigned to! Is there any other way of uploading that doesn't involve HtmlInputFile class?
Thanks
Keith

 
Old June 30th, 2004, 06:54 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

That is a security issue. Consider the following example to see what I mean:
Code:
<html>
<head>
  <script type="text/javascript">
    function DoSubmit()
    {
      document.forms[0].submit();
    }
  </script>
</head>
<body onload="DoSubmit();">
  <form name="frmSubmit" action="StealMyDoc.aspx">
    <input type="file" name="txtFile" value="C:\My Documents\MySecretFile.doc" />
  </form>
</body>
</html>
If you think about this for a while, I think you'll be glad this isn't possible.....

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old June 30th, 2004, 07:02 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,101
Thanks: 0
Thanked 2 Times in 2 Posts
Default

No tif your trying to write a .pwl stealer :)

Hal Levy
Web Developer, PDI Inc.

NOT a Wiley/Wrox Employee
 
Old June 30th, 2004, 08:04 AM
krp krp is offline
Registered User
 
Join Date: Jun 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,
Thanks for the explanation. But surely there must be some way around this for use in a trusted environment.

Has anybody got any suggestions?

Thanks
Keith

 
Old June 30th, 2004, 08:20 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Never trust HTTP.

Of course, if you are in a trusted environment (like an intranet/ office LAN), then you more than likely have direct access to the actual user's machine. So why not have the server make a network file request to the user's computer? The server variables will contain the user's IP address, so you could just make a request to their machine directly (in theory at least). That may sound absurd, but isn't that basically what you are asking for?
 
Old June 30th, 2004, 08:44 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

Well, I got u wanna code to upload.
So if u dont wanna make a Upload form for user, there r some Software like CuteFTP.
or use ur [u]Host Control Panel</u> to upload ur files.

HTH, & keep in touch in any faced problem.

Always:),
Hovik Melkomian.
 
Old June 30th, 2004, 11:56 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,101
Thanks: 0
Thanked 2 Times in 2 Posts
Default

You could write your own Client Side program that has rights to the drive and have the user install that. It can probably do what you want.

Or, you could just have them browse to it..

Hal Levy
Web Developer, PDI Inc.

NOT a Wiley/Wrox Employee





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 in ASP.net problem lpastor ADO.NET 1 September 15th, 2004 08:55 AM





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