Table of Contents

savetoweb

versions0095+
contributorsseltar
started on2006-03-02 06:48

This hack is for saving files to a webserver through an applet.

I've used it to save postscript, jpg, and text-files.

the saveToWeb_ functions are all examples of how to save different files, the one just called saveFile is the most dynamic of them all, and handles byte-arrays.

Syntax:

postToWeb ( String Title, String File-Extension, String SaveFolder, byte[] Data, boolean Open file when done);

And remember, when you select a folder to write to, it has to be writable.

There are tons of ways to set rights, so however you do it, change the rights to the folder you are going to save to to 777 (chmod 777)


// i.e. \\

url = "http://processing.org/"; // the saveFile.php has to be here \\



// then i want to save all jpgs to a folder called savedJPG \\

saveToWeb_saveJPG("mytitle","savedJPG",get(0,0,width,height));

P.S.

For saving a jpg, you have to use this code.

Source code

 
/**
 
savetoweb taken from http://processinghacks.com/hacks:savetoweb
 
@author Yonas Sandbæk
 
*/
 
 
 
String url = "yourUrlToThePHPFile";  // i.e. http://www.google.com/ (with a slash on the end)
 
 
 
void saveToWeb_saveFileString(String title, String ext, String folder, String[] data, boolean popup)
 
{
 
  println("SAVING File START");  
 
  postData(title+"_"+year()+nf(month(),2)+nf(day(),2)+"-"+nf(hour(),2)+nf(minute(),2)+nf(second(),2),ext,folder,join(data,"\n").getBytes(),popup);
 
  println("SAVING File STOP");  
 
}
 
 
 
void saveToWeb_saveFile(String title, String ext, String folder, byte[] data, boolean popup)
 
{
 
  println("SAVING File START");  
 
  postData(title+"_"+year()+nf(month(),2)+nf(day(),2)+"-"+nf(hour(),2)+nf(minute(),2)+nf(second(),2),ext,folder,data,popup);
 
  println("SAVING File STOP");  
 
}
 
 
 
// For saving a jpg, you have to use [[hacks:saveasjpg|this]] code.
 
void saveToWeb_saveJPG(String title, String folder, PImage src)
 
{
 
  println("SAVING JPG START");  
 
  postData(title+"_"+year()+nf(month(),2)+nf(day(),2)+"-"+nf(hour(),2)+nf(minute(),2)+nf(second(),2),"jpg",folder,bufferImage(src),true);
 
  println("SAVING JPG STOP");  
 
}
 
 
 
void postData(String title, String ext, String folder,  byte[] bytes, boolean popup)
 
{
 
  try{
 
    URL u = new URL(url+"saveFile.php?title="+title+"&ext="+ext+"&folder="+folder);
 
    URLConnection c = u.openConnection();
 
 
 
    // post multipart data
 
    c.setDoOutput(true);
 
    c.setDoInput(true);
 
    c.setUseCaches(false);
 
 
 
    // set request headers
 
    c.setRequestProperty("Content-Type", "multipart/form-data; boundary=AXi93A");
 
 
 
    // open a stream which can write to the url
 
    DataOutputStream dstream = new DataOutputStream(c.getOutputStream());
 
 
 
    // write content to the server, begin with the tag that says a content element is comming
 
    dstream.writeBytes("--AXi93A\r\n");
 
 
 
    // discribe the content
 
    dstream.writeBytes("Content-Disposition: form-data; name=\"data\"; filename=\"whatever\" \r\nContent-Type: image/jpeg\r\nContent-Transfer-Encoding: binary\r\n\r\n");
 
    dstream.write(bytes,0,bytes.length);
 
 
 
    // close the multipart form request
 
    dstream.writeBytes("\r\n--AXi93A--\r\n\r\n");
 
    dstream.flush();
 
    dstream.close();
 
 
 
    // read the output from the URL
 
    try{
 
      BufferedReader in = new BufferedReader(new InputStreamReader(c.getInputStream()));
 
      String sIn = in.readLine();
 
      boolean b = true;
 
 
 
      while(sIn!=null){
 
        if(sIn!=null){
 
          if(popup) if(sIn.substring(0,folder.length()).equals(folder)) link(url+sIn, "_blank"); 
 
          System.out.println(sIn);
 
        }
 
        sIn = in.readLine();
 
      }
 
    }
 
    catch(Exception e){
 
      e.printStackTrace();
 
    }
 
  }
 
  catch(Exception e){
 
    e.printStackTrace();
 
  }
 
}
 
 
 
 
 
//--------------------------------
 
//PHP FILE:
 
<?
 
$Title = $_GET['title'];
 
$Ext = $_GET['ext'];
 
$folder = $_GET['folder'];
 
$savepath = dirname($_SERVER["PATH_TRANSLATED"]);
 
 
 
$filename = $Title.".".$Ext;
 
while(file_exists($folder."/".$filename))
 
  $filename = $Title."-".rand(2,500).".".$Ext;
 
 
 
 if (is_uploaded_file($data))
 
 {
 
   $newfile = $savepath."/".$folder."/".$filename;
 
   if (!copy($data, $newfile))
 
   {
 
      // if an error occurs the file could not
 
      // be written, read or possibly does not exist
 
      echo "Error #1 Uploading File.";
 
      exit();
 
   }else{
 
      echo $folder."/".$filename;
 
   }
 
 }else{
 
      echo "Error #2 Uploading File.";
 
      exit();
 
 }
 
?>
 
 

Related Links

hacks/savetoweb.txt · Last modified: 2008-07-04 12:38 by seltar