Wrox Programmer Forums
|
ASP.NET 1.0 and 1.1 Professional For advanced ASP.NET 1.x coders. Beginning-level questions will be redirected to other forums. NOT for "classic" ASP 3 or the newer ASP.NET 2.0 and 3.5
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Professional 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 July 25th, 2004, 04:48 AM
Authorized User
 
Join Date: Jul 2003
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default file upload script problem

Below is my fileupload script..

problem : When i run it on my server (localhost) with folder name c:\articles it works perfectly. Now i want to upload it to server of www.tushar.com and want to store the files in http://www.tushar.com/articles folder. When i type above path in Mypath="http://www.tushar.com/articles" it wont work..
Even though i hv created the folder article on server...

What path shud i write to make it work..

Plz help me



<%@ Page Language="vb" %>
<html>
    <Script Language="VB" RunAt="Server">

Sub Page_Load(Sender as Object, e as EventArgs)

    Dim MyPath, MyName as string
    ' Display the names in C:\ that represent directories.

    MyPath = "C:\article" ' Set the path.
    MyName = Dir(MyPath, vbDirectory) ' Retrieve the first entry.

    if MyName="" ' The folder is not there & to be created
        MkDir("c:\article\") 'Folder created
       span2.InnerHtml="A Folder (c:\article) is created at the Page_Load"
   end if

End Sub

      Sub Upload_Click(Sender as Object, e as EventArgs)

         ' Display properties of the uploaded file

         FileName.InnerHtml = MyFile.PostedFile.FileName
         FileContent.InnerHtml = MyFile.PostedFile.ContentType
         FileSize.InnerHtml = MyFile.PostedFile.ContentLength
         UploadDetails.visible = True

' Let us recover only the file name from its fully qualified path at client

        Dim strFileName as string
        strFileName = MyFile.PostedFile.FileName
        Dim c as string = System.IO.Path.GetFileName(strFileName) ' only the attched file name not its path

                 ' Let us Save uploaded file to server at C:\ServerFolder

                 Try

           MyFile.PostedFile.SaveAs("c:\article" + c)
                        Span1.InnerHtml = "Your File Uploaded Sucessfully at server " & c

                catch Exp as exception
                  span1.InnerHtml = "An Error occured. Please check the attached file"
                        UploadDetails.visible = false
                       span2.visible=false
                End Try

      End Sub
    </Script>
    <Body>
        <B>Uploading In ASP.Net - A Rich
            Uploading !

        <P>
            <Form Method="Post" EncType="Multipart/Form-Data" RunAt="Server">
                Choose Your File To Upload :
                <BR>
                <Input ID="MyFile" Type="File" RunAt="Server" Size="40">
                <BR>
                <BR>
                <Input Type="Submit" Value="Upload" OnServerclick="Upload_Click" RunAt="Server">
                <P>
                    <Div ID="UploadDetails" Visible="False" RunAt="Server">
                        File Name:
                        <Span ID="FileName" RunAt="Server" />
                        <BR>
                        File Content:
                        <Span ID="FileContent" RunAt="Server" />
                        <BR>
                        File Size:
                        <Span ID="FileSize" RunAt="Server" />bytes
                        <BR>
                    </Div>
                    <Span ID="Span1" Style="Color:Red" RunAt="Server" />
                    <Span ID="Span2" Style="Color:Red" RunAt="Server" />
            </Form>

    </Body>
</html>


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

"http://www.tushar.com/articles" is not a real path. It's a virtual path. The local file system doesn't know how to handle this. You need to determine the local physical file path that is associated with this virtual path. You do this with the MapPath() method of the Server class. Also you should always deal with files in this way using a application relative path.

strRealDirPath = Server.MapPath("~/article")

This will get you the physical directory of the virtual directory "article" of your web application regardless of where the application is running. As long as you have the correct file access rights (in the OS and from the web server) you should then be able to save files to that physical path. Use the same code when retrieving the files.
 
Old July 25th, 2004, 12:20 PM
Authorized User
 
Join Date: Jul 2003
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I modified two sentence as
MyPath = Server.MapPath("~/article")

and

MyFile.PostedFile.SaveAs("Mypath" + c)

Please check the url
www.ieeevesit.org/upload/upload.aspx

It is not working giving me the error...

Plz check it..

Thank you for your reply


 
Old July 25th, 2004, 05:20 PM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 336
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to alyeng2000
Default

could you show us the exception message

Ahmed Ali
Software Developer
 
Old July 26th, 2004, 06:56 AM
Authorized User
 
Join Date: Jul 2003
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default

"An Error occured. Please check the attached file"

This error it shows.. You can check it on www.ieeevesit.org/upload/upload.aspx

 
Old July 26th, 2004, 09:22 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Unfortunately, that error message doesn't tell us anything. You need to be provide with the exact message from the exception that is occurring in order for us to help you.

Why do you have "MyPath" in quotes? It's a variable. Try this:

MyFile.PostedFile.SaveAs(Mypath + "\" + c)
 
Old July 26th, 2004, 10:59 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

Let me help u by an example:
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>
HTH.

Always:),
Hovik Melkomian.
 
Old November 30th, 2006, 05:08 PM
Registered User
 
Join Date: Nov 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi, is it possible to pass a variable to the Path
in the section of your code

Server.MapPath("VariableFromForm\\")

if (strCurrentFileExtension == ".gif" || strCurrentFileExtension == ".jpg")
   {
objCurrentFile.SaveAs(Server.MapPath("books\\") + strCurrentFileName);

strMessage.Append(strCurrentFileName + " successfully uploaded.<BR>");
   }

The Variable being a last name etc that was also used to create the directory?







Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem to upload file imjigu2001 ASP.NET 2.0 Professional 2 March 26th, 2008 04:37 AM
Upload file to server from ASP script crmpicco Classic ASP Components 2 June 14th, 2006 01:25 AM
asp file upload script having problems with MYSQL paulmcn Classic ASP Databases 0 September 16th, 2005 01:26 PM
Script to upload text file to SQL with errors ss2003 Beginning PHP 2 March 10th, 2004 12:00 PM
Problem with upload script jeremy108 BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5 4 July 2nd, 2003 01:48 PM





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