Re: [xwiki-users] Reading a file submitted from a form

2014-12-09 Thread Hamster
The joy of zero-based vs one-based arrays :-)



--
View this message in context: 
http://xwiki.475771.n2.nabble.com/Reading-a-file-submitted-from-a-form-tp7593368p7593394.html
Sent from the XWiki- Users mailing list archive at Nabble.com.
___
users mailing list
users@xwiki.org
http://lists.xwiki.org/mailman/listinfo/users


Re: [xwiki-users] Reading a file submitted from a form

2014-12-09 Thread Bryn Jeffries
I think the problem has more to do with the FileUploadPlugin returning an array 
containing entries for all form field items, not just the file inputs. Also, 
the FileItem getFile(FormFieldName) method in the plugin is not exposed in the 
FileUploadPlugin API, so I had to resort to getting the whole ListFileItem 
and iterating through to finding one with a matching field instead of assuming 
I knew the correct index.

I'm keeping my fingers crossed that the XWiki folks don't start using one-based 
arrays /shudder

 -Original Message-
 From: Hamster [mailto:teun...@hotmail.com]
 Sent: Tuesday, 9 December 2014 9:04 PM
 To: users@xwiki.org
 Subject: Re: [xwiki-users] Reading a file submitted from a form
 
 The joy of zero-based vs one-based arrays :-)
 


___
users mailing list
users@xwiki.org
http://lists.xwiki.org/mailman/listinfo/users


[xwiki-users] Reading a file submitted from a form

2014-12-09 Thread Bryn Jeffries
I think the problem has more to do with the FileUploadPlugin returning an array 
containing entries for all form field items, not just the file inputs. Also, 
the FileItem getFile(FormFieldName) method in the plugin is not exposed in the 
FileUploadPlugin API, so I had to resort to getting the whole ListFileItem 
and iterating through to finding one with a matching field instead of assuming 
I knew the correct index.

I'm keeping my fingers crossed that the XWiki folks don't start using one-based 
arrays /shudder

 -Original Message-
 From: Hamster [mailto:teun...@hotmail.com]
 Sent: Tuesday, 9 December 2014 9:04 PM
 To: users@xwiki.org
 Subject: Re: [xwiki-users] Reading a file submitted from a form

 The joy of zero-based vs one-based arrays :-)




___
users mailing list
users@xwiki.org
http://lists.xwiki.org/mailman/listinfo/users


Re: [xwiki-users] Reading a file submitted from a form

2014-12-07 Thread Bryn Jeffries
I wrote:
 [...] I'd like to perform a simple check that I can access the file (by 
 returning the file size and a line-by-line echo) using a velocity script:
 {{velocity}}
 #if ($request.addFile == true)
   {{info}}Request received to add file{{/info}}
   #set ($fileUpload = $xwiki.fileupload)
   #set ($newFileName = $fileUpload.getFileName(newfile))
   #if (!$newFileName)
 {{warn}}Failed to find submitted file{{/warn}}
   #else
 Found file $newFileName to load
 #set ($discard = $fileUpload.loadFileList())
 #set ($fileitem = $fileUpload.getFileItems().get(0))

It was this last line, which I'd lifted from the script in OfficeImporter, that 
was causing the bother. The index of the file I wanted was 1, not 0.

Move along now, nothing to see...
___
users mailing list
users@xwiki.org
http://lists.xwiki.org/mailman/listinfo/users


[xwiki-users] Reading a file submitted from a form

2014-12-05 Thread Bryn Jeffries
Hi,

I have a form as follows:
{{html}}
form action= enctype=multipart/form-data method=post
input type=hidden name=addFile value=true /
 input type=file id=myupload name=newfile /
 input type=submit
/form
{{/html}}

And I'd like to perform a simple check that I can access the file (by returning 
the file size and a line-by-line echo) using a velocity script:
{{velocity}}
## If this page is called with addStudy=true then do something with the 
information
#if ($request.addFile == true)
  {{info}}Request received to add file{{/info}}
  #set ($fileUpload = $xwiki.fileupload)
  #set ($newFileName = $fileUpload.getFileName(newfile))
  #if (!$newFileName)
{{warn}}Failed to find submitted file{{/warn}}
  #else
Found file $newFileName to load
#set ($discard = $fileUpload.loadFileList())
#set ($fileitem = $fileUpload.getFileItems().get(0))
Found file item for $fileitem.getFieldName() to load, of 
$fileitem.getSize() bytes
#set ($filestream = $fileitem.getInputStream())
{{info}}filestream.getText(){{/info}}
$filestream.getText()
{{info}}filestream.readLines(){{/info}}
#foreach($line in $filestream.readLines())
| $line
#end
  #end
#end
{{/velocity}}

And also with a Groovy script:
{{groovy}}
if(request.addFile == true) {
  println {{info}}addFile requested {{/info}}
  fileUpload = xwiki.fileupload
  newFileName = fileUpload.getFileName(newfile)
  if(newFileName != null) {
println Found file $newFileName to load
fileUpload.loadFileList()
fileitem = fileUpload.getFileItems().get(0)
println Found file item for ${fileitem.getFieldName()} to load, of 
${fileitem.getSize()} bytes
filestream = fileitem.getInputStream()

println {{info}}filestream.getText(){{/info}}
println filestream.getText()

println {{info}}filestream.eachLine{{/info}}
filestream.eachLine {
  println ${it}
}
  } else {
 println Failed to find submitted file
  }
}
{{/groovy}}

I've tested this page out with a 3kb text file, and both scripts tell me I have 
a 4 byte file and I get no lines returned. I'm sure I'm being a total noob, but 
can somebody please put me out of my misery and explain why this isn't working?

___
users mailing list
users@xwiki.org
http://lists.xwiki.org/mailman/listinfo/users