Skip to content

Instantly share code, notes, and snippets.

@tomchiverton
Forked from jsteenkamp/upload.cfc
Created September 19, 2012 11:50
Show Gist options
  • Select an option

  • Save tomchiverton/3749253 to your computer and use it in GitHub Desktop.

Select an option

Save tomchiverton/3749253 to your computer and use it in GitHub Desktop.

Revisions

  1. tomchiverton revised this gist Sep 19, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion upload.cfc
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@

    <cffunction name="upload" access="remote" returntype="struct" returnformat="json" output="false">
    <cfscript>
    var uploadDir = expandPath('.') & '/uploads/'; // should be a temp directory that you clear periodically to flush orphaned files
    var uploadDir = getTempDirectory(); // should be a temp directory that you clear periodically to flush orphaned files
    var uploadFile = uploadDir & arguments.NAME;
    var response = {'result' = arguments.NAME, 'id' = 0};
    var result = {};
  2. @jsteenkamp jsteenkamp revised this gist Jul 30, 2011. 1 changed file with 51 additions and 50 deletions.
    101 changes: 51 additions & 50 deletions upload.cfc
    Original file line number Diff line number Diff line change
    @@ -1,59 +1,60 @@
    <cfcomponent>

    <cffunction name="upload" access="remote" returntype="struct" returnformat="json" output="false">
    <cfscript>
    var uploadDir = expandPath('.') & '/uploads/'; // should be a temp directory that you clear periodically to flush orphaned files
    var uploadFile = uploadDir & arguments.NAME;
    var response = {'result' = arguments.NAME, 'id' = 0};
    var result = {};
    // if chunked append chunk number to filename for reassembly
    if (structKeyExists(arguments, 'CHUNKS')){
    uploadFile = uploadFile & '.' & arguments.CHUNK;
    response.id = arguments.CHUNK;
    }
    </cfscript>
    <cffunction name="upload" access="remote" returntype="struct" returnformat="json" output="false">
    <cfscript>
    var uploadDir = expandPath('.') & '/uploads/'; // should be a temp directory that you clear periodically to flush orphaned files
    var uploadFile = uploadDir & arguments.NAME;
    var response = {'result' = arguments.NAME, 'id' = 0};
    var result = {};
    // if chunked append chunk number to filename for reassembly
    if (structKeyExists(arguments, 'CHUNKS')){
    uploadFile = uploadFile & '.' & arguments.CHUNK;
    response.id = arguments.CHUNK;
    }
    </cfscript>

    <!--- save file data from multi-part form.FILE --->
    <cffile action="upload" result="result" filefield="FILE" destination="#uploadFile#" nameconflict="overwrite"/>
    <!--- save file data from multi-part form.FILE --->
    <cffile action="upload" result="result" filefield="FILE" destination="#uploadFile#" nameconflict="overwrite"/>

    <cfscript>
    // Example: you can return uploaded file data to client
    response['size'] = result.fileSize;
    response['type'] = result.contentType;
    response['saved'] = result.fileWasSaved;
    // Example: you can return uploaded file data to client
    response['size'] = result.fileSize;
    response['type'] = result.contentType;
    response['saved'] = result.fileWasSaved;

    // reassemble chunked file
    if (structKeyExists(arguments, 'CHUNKS') && arguments.CHUNK + 1 == arguments.CHUNKS){
    try {
    var uploadFile = uploadDir & arguments.NAME; // file name for reassembled file - if using a temp directory then this should be the final output path/file
    if (fileExists(uploadFile)){
    fileDelete(uploadFile); // delete otherwise append will add chunks to an existing file
    }
    var tempFile = fileOpen(uploadFile,'append');
    for (var i = 0; i < arguments.CHUNKS; i++) {
    var chunk = fileReadBinary('#uploadDir#/#arguments.NAME#.#i#');
    fileDelete('#uploadDir#/#arguments.NAME#.#i#');
    fileWrite(tempFile, chunk);
    }
    fileClose(tempFile);
    }
    catch(any err){
    // clean up chunks for incomplete upload
    var d = directoryList(uploadDir,false,'name');
    if (arrayLen(d) != 0){
    for (var i = 1; i <= arrayLen(d); i++){
    if (listFirst(d[i]) == arguments.NAME && val(listLast(d[i])) != 0){
    fileDelete('#uploadDir##d[i]#');
    }
    }
    }
    // you could add more error handling and return info from err
    response = {'error' = {'code' = 500, 'message' = 'Internal Server Error'}, 'id' = 0};
    }
    }
    </cfscript>
    // reassemble chunked file
    if (structKeyExists(arguments, 'CHUNKS') && arguments.CHUNK + 1 == arguments.CHUNKS){
    try {
    var uploadFile = uploadDir & arguments.NAME; // file name for reassembled file - if using a temp directory then this should be the final output path/file
    if (fileExists(uploadFile)){
    fileDelete(uploadFile); // delete otherwise append will add chunks to an existing file
    }

    <cfreturn response/>
    </cffunction>
    var tempFile = fileOpen(uploadFile,'append');
    for (var i = 0; i < arguments.CHUNKS; i++) {
    var chunk = fileReadBinary('#uploadDir#/#arguments.NAME#.#i#');
    fileDelete('#uploadDir#/#arguments.NAME#.#i#');
    fileWrite(tempFile, chunk);
    }
    fileClose(tempFile);
    }
    catch(any err){
    // clean up chunks for incomplete upload
    var d = directoryList(uploadDir,false,'name');
    if (arrayLen(d) != 0){
    for (var i = 1; i <= arrayLen(d); i++){
    if (listFirst(d[i]) == arguments.NAME && val(listLast(d[i])) != 0){
    fileDelete('#uploadDir##d[i]#');
    }
    }
    }

    // you could add more error handling and return info from err
    response = {'error' = {'code' = 500, 'message' = 'Internal Server Error'}, 'id' = 0};
    }
    }
    return response;
    </cfscript>
    </cffunction>

    </cfcomponent>
  3. @jsteenkamp jsteenkamp created this gist Jul 30, 2011.
    59 changes: 59 additions & 0 deletions upload.cfc
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,59 @@
    <cfcomponent>

    <cffunction name="upload" access="remote" returntype="struct" returnformat="json" output="false">
    <cfscript>
    var uploadDir = expandPath('.') & '/uploads/'; // should be a temp directory that you clear periodically to flush orphaned files
    var uploadFile = uploadDir & arguments.NAME;
    var response = {'result' = arguments.NAME, 'id' = 0};
    var result = {};
    // if chunked append chunk number to filename for reassembly
    if (structKeyExists(arguments, 'CHUNKS')){
    uploadFile = uploadFile & '.' & arguments.CHUNK;
    response.id = arguments.CHUNK;
    }
    </cfscript>

    <!--- save file data from multi-part form.FILE --->
    <cffile action="upload" result="result" filefield="FILE" destination="#uploadFile#" nameconflict="overwrite"/>

    <cfscript>
    // Example: you can return uploaded file data to client
    response['size'] = result.fileSize;
    response['type'] = result.contentType;
    response['saved'] = result.fileWasSaved;

    // reassemble chunked file
    if (structKeyExists(arguments, 'CHUNKS') && arguments.CHUNK + 1 == arguments.CHUNKS){
    try {
    var uploadFile = uploadDir & arguments.NAME; // file name for reassembled file - if using a temp directory then this should be the final output path/file
    if (fileExists(uploadFile)){
    fileDelete(uploadFile); // delete otherwise append will add chunks to an existing file
    }
    var tempFile = fileOpen(uploadFile,'append');
    for (var i = 0; i < arguments.CHUNKS; i++) {
    var chunk = fileReadBinary('#uploadDir#/#arguments.NAME#.#i#');
    fileDelete('#uploadDir#/#arguments.NAME#.#i#');
    fileWrite(tempFile, chunk);
    }
    fileClose(tempFile);
    }
    catch(any err){
    // clean up chunks for incomplete upload
    var d = directoryList(uploadDir,false,'name');
    if (arrayLen(d) != 0){
    for (var i = 1; i <= arrayLen(d); i++){
    if (listFirst(d[i]) == arguments.NAME && val(listLast(d[i])) != 0){
    fileDelete('#uploadDir##d[i]#');
    }
    }
    }
    // you could add more error handling and return info from err
    response = {'error' = {'code' = 500, 'message' = 'Internal Server Error'}, 'id' = 0};
    }
    }
    </cfscript>

    <cfreturn response/>
    </cffunction>

    </cfcomponent>