Writing a file upload function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • honey99
    New Member
    • Jul 2007
    • 46

    Writing a file upload function

    hi!

    i have a table..which consists of a upload and browse buttons and some place to display an image.if i browse a file(jpg,gif) and upload,it should appear in place of image in that table itself.my code is

    [CODE=html]<form method="POST" ENCTYPE="multip art/form-data" ACTION="abc.Act ion">
    <table cellpadding=0 cellspacing=0 border=0 style='top:40px ;left:0px;posit ion:absolute;z-index:0;' width=130px >

    <TR>
    <TD vAlign=top width=8 bgColor=white>< IMG src="../../images/a.gif"></TD>
    <TD background="../../images/b.gif" ></TD>
    <TD vAlign=top align=right width=20 bgColor=white>< IMG src="../../images/patreg/c.gif"></TD>
    </TR>
    <TR >
    <TD background="../../images/d.gif" nowrap></TD>
    <TD bgcolor=#dbeaf5 >

    <table height=250px><t r><td valign=top>
    <img src="../../g.gif" style="height:1 00px;width:100p x;" > </td></tr>

    <tr><td valign=bottom>< input type="file" name="file" id="uploadme" size=1 />
    <input type="button" value="UpLoad" onclick='javasc ript:UploadFile ();'class=Butto nStyles align=bottom></td></tr>
    </table>
    </td>
    <TD background="../../images/e.gif" ></TD>
    </tr><tr>
    <TD vAlign=bottom width=8 bgColor=white>< IMG src="../../images/f.gif"></TD>
    <TD background="../../images/g.gif" ></TD>
    vAlign=bottom align=right width=20 bgColor=white>< IMG src="../../images/h.gif"></TD>
    </TR>
    </TBODY>
    </table>
    Code:
    <form method="POST" ENCTYPE="multipart/form-data" ACTION="abc.Action">
    wht should i write in the uploadFile() function???
    Last edited by pbmods; Aug 29 '07, 09:58 AM. Reason: Changed [CODE] to [CODE=html].
  • dmjpro
    Top Contributor
    • Jan 2007
    • 2476

    #2
    Hi Honey!
    You should use Proper Code tags.
    Anyway you need not to have your own function.
    There is FILE tag in HTML.
    Look for help in Google on File HTML Tag.
    Best of luck.

    Kind regards,
    Dmjpro.

    Comment

    • pbmods
      Recognized Expert Expert
      • Apr 2007
      • 5821

      #3
      Heya, Honey99.

      Changed thread title to better describe the problem.

      Simply submit the form to upload the file.

      Comment

      • honey99
        New Member
        • Jul 2007
        • 46

        #4
        Originally posted by pbmods
        Heya, Honey99.

        Changed thread title to better describe the problem.

        Simply submit the form to upload the file.

        no...actually what i want is when i upload an image file it should be displayed in that table itself..can anybody give me an example for uploading an image

        Comment

        • acoder
          Recognized Expert MVP
          • Nov 2006
          • 16032

          #5
          Originally posted by honey99
          no...actually what i want is when i upload an image file it should be displayed in that table itself..can anybody give me an example for uploading an image
          To upload an image, you will need the HTML input type=file element on the client-side, the form must be posted with the enctype changed to "multipart/form-data" and a server-side script which moves it to the upload location.

          If you want to display in that table, submit the form to the same page and let the server-side script deal with changing the image. Now if you want to do this without reloading the page, you will need to use an iframe.

          Comment

          • pbmods
            Recognized Expert Expert
            • Apr 2007
            • 5821

            #6
            Heya, Honey99.

            You can set the src of an image to the local path of the image file (by getting the value of the file input), but for security reasons, many browsers specifically prevent this.

            I think at this point, only IE allows you to do that out-of-the-box.

            Note that for security reasons, the User has to submit the form to upload the file. This ensures that a malicious script cannot upload files without the User's consent.

            Once the User has submitted the form, you can redirect back to the form and output an image tag that displays the newly-uploaded image.

            Comment

            • honey99
              New Member
              • Jul 2007
              • 46

              #7
              thank u for u r guidelines....i need an example to upload an image in the same page....can anybody post me an example codefor this...

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                Originally posted by honey99
                thank u for u r guidelines....i need an example to upload an image in the same page....can anybody post me an example codefor this...
                Without refreshing/reloading the page or just to the same page?

                Comment

                • honey99
                  New Member
                  • Jul 2007
                  • 46

                  #9
                  i'll be thankful to you if u send me the example code without refreshing the page...

                  Comment

                  • acoder
                    Recognized Expert MVP
                    • Nov 2006
                    • 16032

                    #10
                    Originally posted by honey99
                    i'll be thankful to you if u send me the example code without refreshing the page...
                    Why reinvent the wheel? Try this link or search for "ajax file upload".

                    Comment

                    • honey99
                      New Member
                      • Jul 2007
                      • 46

                      #11
                      Originally posted by acoder
                      Why reinvent the wheel? Try this link or search for "ajax file upload".
                      the link you gave has PHP source code....but i want JSP code to upload image in same page wothout refreshing the page

                      Comment

                      • acoder
                        Recognized Expert MVP
                        • Nov 2006
                        • 16032

                        #12
                        Originally posted by honey99
                        the link you gave has PHP source code....but i want JSP code to upload image in same page wothout refreshing the page
                        If you want JSP, ask in the Java forum. It doesn't matter what the sever-side language is. The example allows you to upload the image without unloading the page. Just convert the PHP to JSP.

                        Comment

                        • pbmods
                          Recognized Expert Expert
                          • Apr 2007
                          • 5821

                          #13
                          Heya, Honey99.

                          The idea behind doing an "AJAX" file upload is that, since you HAVE to submit a form to upload files, you put the form in an iframe and disguise it to look like it's part of the page.

                          Then, when the User submits the form, you load another page in the iframe that calls some JavaScript that updates the src of the preview image to the URL of the file he just uploaded.

                          Comment

                          • pbmods
                            Recognized Expert Expert
                            • Apr 2007
                            • 5821

                            #14
                            Have a look at this article.

                            Comment

                            Working...