0

which grails plugin should I use to upload large video files, like 20MB, 30MB, 100MB, I want progress bar also to be there, I have tried to use Super file upload plugin for this, but I think I am doing something wrong in it, it ain't working for me, should I go with this, or some other more easy to use plugin available out there, I have seen Jquery plugin too, but as I only have to add one video in one time, I think super file upload plugin will be good for me.

I have tried it, but getting error, please find below for the code.

Although for the error on the super file upload plugin:

in my GSP I have:

    <sfu:generateConfiguration fileSize="300" form="bookForm"  buttonWidth="104" buttonHeight="30"/>

<form class="form-horizontal" id="bookForm" name="saveVideoFile" action="saveVideoFile" onsubmit="return sfuSubmitForm(this);">
        <div class="control-group">
            <label class="control-label" for="inputEmail">Select Category</label>
            <div class="controls">

            <label class="control-label" for="inputPassword">Upload file</label>
        <div class="control-group">
            <div class="controls">
                 Choose file: 
                 <sfu:fileUploadControl></sfu:fileUploadControl>
                    <br/>
                    Progress bar: <sfu:fileUploadProgressBar/>
                    <br/>
            </div>
        </div>
          <input type="submit" value="Save">
    </form>

and then in the controller saveVideoFile action:

def saveVideoFile(){
    String uploadFilename = params.uploadedFileId
    println("params=${params}")

    String fileUploaded

    if ( uploadFilename ) { // get the full path name of the file from the temp directory
        def file = superFileUploadService.getTempUploadFile(uploadFilename)

        fileUploaded = fileUploadService.uploadFile( file, "newFile.jpg", "assets/uploadFile/" );
        println("fileUploaded=${fileUploaded}")

    }else{ // file was not uploaded by flash. User might have javascript off
        def fileStream = request.getFile('sfuFile'); // handle normal file upload as per grails docs
        fileUploaded = fileUploadService.uploadFile( fileStream, "newFile.jpg", "assets/uploadFile/" );
        render "Nothing to upload"
    }


}

and I have a function in the service for saving the file object:

 String uploadFile( File file, String name, String destinationDirectory ) {

        def serveletContext = ServletContextHolder.servletContext
        def storagePath = serveletContext.getRealPath( destinationDirectory )

        def storagePathDirectory = new File( storagePath )

        if( !storagePathDirectory.exists() ){
            println("creating directory ${storagePath}")
            if(storagePathDirectory.mkdirs()){
                println "SUCCESS"   
            }else{
                println "FAILED"
            }
        } 

        // Store file

        println("file In service=${file}")
        //def tempFile = file.getBytes()
        def tempFile = file
        if(!tempFile?.isEmpty()){
            tempFile.transferTo( new File("${storagePath}/${name}") )
            println("Saved File: ${storagePath}/${name}")
            return "${storagePath}/${name}" 
        }else{
            println "File: ${tempFile.inspect()} was empty"
            return null
        }
}

On save it is giving:

No signature of method: java.io.File.isEmpty() is applicable for argument types: () values: [] Possible solutions: identity(groovy.lang.Closure), isFile(), list(), dump(), inspect()

at line:

if(!tempFile?.isEmpty()){

on the service function.

and on printing tempFile variable, I am getting temp storage location path.

I know I am doing something wrong, any help will be useful.

Saurabh Dixit
  • 633
  • 4
  • 16
  • Clearly there is no `isEmpty()` for `File`. Use `if(tempFile.size()){..}` when `tempFile` represents the jpg file. – dmahapatro Jul 01 '13 at 15:40
  • but I have to upload video files, and even if I remove that condition then also I will error on the line: tempFile.transferTo( new File("${storagePath}/${name}") ) – Saurabh Dixit Jul 01 '13 at 15:42
  • `def file = superFileUploadService.getTempUploadFile(uploadFilename)` returns an absolute file or a file location? – dmahapatro Jul 01 '13 at 15:49
  • a file location something like c:\\templocation\789798798-7879-798798 – Saurabh Dixit Jul 01 '13 at 15:50
  • Error msg on the console: No signature of method: java.io.File.isEmpty() is applicable for argument types: () values: [] Possible solutions: identity(groovy.lang.Closure), isFile(), list(), dump(), inspect() and file prints: c:\swf_temp\0000013f-9af0-40c4-8087-57dd4fe29610 – Saurabh Dixit Jul 01 '13 at 15:54

1 Answers1

0

Found the issue with my script, I was trying to invoke file as Multipartfile object, super file uploader gives a temporary location of the file on the disk, now by copy function it can be moved to specified directory:

If I change the conditional block in my service:

if(tempFile?.isFile()){
                String sourceFilePath = tempFile
                String destinationFilePath = "${storagePath}/${name}"
                (new AntBuilder()).copy(file: sourceFilePath, tofile: destinationFilePath)

                println("Saved File: ${storagePath}/${name}")
                return "${storagePath}/${name}" 
            }else{
                println "File: ${tempFile.inspect()} was empty"
                return null
            }

then it will be saved, I had to copy the file from the temporary location to actual directory. some more enhancements will be required, but for the above code it will work this way.

Saurabh Dixit
  • 633
  • 4
  • 16