如何从GWT FileUpload组件中检索文件? [英] How to retrieve file from GWT FileUpload component?
问题描述
我试过了,
<$ p $ FileUpload fileUpload = new FileUpload();
filepload.addChangeHandler(new new ChangeHandler(){
@Override $ b $ public void onChange(ChangeEvent event){
//这里我提交表单,并且创建一个响应客户端显示成功消息
}});好吧,到目前为止,我可以上传一个文件,并从servlet中我(我)
正在处理
浏览器 - >文件上传对话框 - >选择文件 - >用文件提交表单到服务器 - >在服务器上处理文件 - >作为响应(字符串)将处理后的文件发回给客户机。
如果您想要避免上述步骤并在浏览器中处理文件,则无法在当前的JavaScript中执行此操作。像Flash,Applet,Silverlight或Activex等并行技术可能会有所帮助。未来的正确方法是使用 HTML5文件apis 。
如果您不希望使用Flash / applet等传统技术那么可以探索FileReader上的HTML5 apis。然而折衷是你需要检查浏览器是否支持api。
HTML5 FileReader
FileReader包含四个选项读取一个文件,异步:
$ b $ pre $ FileReader.readAsBinaryString(Blob | File) - result属性将包含文件/ blob的数据作为二进制字符串。
FileReader.readAsText(Blob | File,opt_encoding) - result属性将包含文件/ blob的数据作为文本字符串。
FileReader.readAsDataURL(Blob | File) - result属性将包含编码为数据URL的文件/ blob数据。
FileReader.readAsArrayBuffer(Blob | File) - result属性将包含文件/ blob的数据作为ArrayBuffer对象。
GWT包装器的示例 -
https://github.com/bradrydzewski/gwt-filesystem
参考 -
>
I want to upload a file using GWT fileUploader component,
I tried like this,
FileUpload fileUpload = new FileUpload();
filepload.addChangeHandler(new new ChangeHandler() {
@Override
public void onChange(ChangeEvent event) {
// here i submit the form, and a response is created to client side to display success message.
} });
- OK, so far i can upload a file and, from servlet i can give a success message.
- I handled it with in onSubmitCompleate event of form panel,
- thanks allot.
- let me ask one more thing, is there anyway i can display the file uploaded, before saving it into the database?
- i.e., actually i need to provide a composite component for uploading a file and text area to tell details about the file.
- and when user choose file for uploading, i want give a display of file uploaded(so that he can ensure correct file is uploaded)
- and it will be saved to db when whole form is submitted.
I am guessing you want to allow user to upload file using GWT Fileupload widget and then do not wish to process it on server side. You want a byte array representation in client side.
Usual steps for File Processing Browser -> File Upload Dialog -> Select File -> Submit Form with File to server -> Process File on Server -> Send back processed file to Client as response ( string ).
If you want to avoid the above steps and process the file in browser there is no way to do it in current javascript. Parallel technologies like Flash, Applet, Silverlight or Activex might help. The correct approach to be pursued in future would be using HTML5 file apis.
If you do not wish to use legacy technology like flash/applet then HTML5 apis on FileReader can be explored. However tradeoff is you need to check whether api is supported across browser.
HTML5 FileReader
FileReader includes four options for reading a file, asynchronously:
FileReader.readAsBinaryString(Blob|File) - The result property will contain the file/blob's data as a binary string.
FileReader.readAsText(Blob|File, opt_encoding) - The result property will contain the file/blob's data as a text string.
FileReader.readAsDataURL(Blob|File) - The result property will contain the file/blob's data encoded as a data URL.
FileReader.readAsArrayBuffer(Blob|File) - The result property will contain the file/blob's data as an ArrayBuffer object.
Example of GWT wrapper over these - https://github.com/bradrydzewski/gwt-filesystem
Reference -
这篇关于如何从GWT FileUpload组件中检索文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!