Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Image upload to a website

  • 12-04-2006 10:53AM
    #1
    Registered Users, Registered Users 2 Posts: 871 ✭✭✭


    Hi I'm trying to add an image upload function to my project, can anyone suggest a good tutorial or resource, (maybe jsp or javascript??). I am trying to add this function to a website so that it will save the image to a predefined folder and rename the image too.


Comments

  • Closed Accounts Posts: 4,655 ✭✭✭Ph3n0m


    look up php or asp - scripts are already available that do precisely what you want

    try searching for

    php image uploader script or something similiar


  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    [PHP]<form enctype="multipart/form-data" action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post">
    <input type="hidden" name="MAX_FILE_SIZE" value="2048000">
    File: <input name="userfile" type="file" /><br />
    <input type="submit" value="Upload" />
    </form>

    <?php
    if (@is_uploaded_file($_FILES[&quot;userfile"]["tmp_name"])) {
    copy($_FILES["userfile"]["tmp_name"], "/images/" . $_FILES["userfile"]["name"]);
    echo "<p>File uploaded successfully.</p>";
    }
    ?>[/PHP]

    Something simple like this should get you started.


  • Registered Users, Registered Users 2 Posts: 130 ✭✭irishfeller


    If you want to use jsp then the following code below is what u need.

    - Put the code in a jsp e.g. processAttachment.jsp
    - Use a multipart form as in example above and point form at the new jsp 'processAttachment.jsp'



    <%@ page import="java.util.Vector"%>
    <%@ page import="java.util.Date"%>
    <%@ page import="java.text.SimpleDateFormat"%>
    <%@ page import="com.oreilly.servlet.MultipartRequest" %>
    <%@ page import="java.io.*" %>
    <%@ page import="java.util.*" %>


    <%@ page session="true"%>

    <%

    String returnMessage = "";
    //String saveDirectory = application.getRealPath("/") + "/attachments/";
    //String relativeDirectory = "../attachments/";
    String saveFile = "";
    String returnTo="listImages.jsp";
    String sql = "";


    boolean saveToDB = true;
    byte dataBytes[] = new byte[1];
    String file = "";
    String contentType = request.getContentType();

    try
    {

    MultipartRequest multi= new MultipartRequest(request,".",5*1024*1024);

    Enumeration files=multi.getFileNames();
    File f=null;
    if(files.hasMoreElements())
    {
    System.out.println("TEST 9");
    String name=(String)files.nextElement();
    saveFile=multi.getFilesystemName(name);

    String type=multi.getContentType(name);
    f=multi.getFile(name);

    InputStream is = new FileInputStream(f);
    byte b[]=new byte[is.available()];
    is.read(b);


    File dir = new File(saveDirectory);
    dir.mkdir();

    FileOutputStream fileOut = new FileOutputStream(saveDirectory +"/" + saveFile);


    fileOut.write(b);
    //fileOut.write(dataBytes, startPos, (endPos - startPos));
    fileOut.flush();
    fileOut.close();

    returnMessage +="<br>File saved as " +saveFile;

    }

    }
    catch(Exception e)
    {
    returnMessage += "<br>An error occurred reading file. Please try again. " + e.getMessage();
    // response.sendRedirect("attachments.jsp?requestId="+requestId+"&returnMessage="+returnMessage);
    saveToDB=false;
    }


  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    ..............

    Thats why I love php! :D


Advertisement