fileUploadField wicket 1.4.8 upload multiple files - file-upload

I have few queries with regard to fileUploadField in wicket 1.4.8. I want the user to have ability to upload one file at a time but should be able to upload many files before form submit.
I have included . User does select one file at a time and I display it as thumbnail on the page. I am simulating the behavior of with a "Choose Image" button. He is then allowed to select another file. So, how do I get all these multiple files in wicket controller on form submit?
2.Most of the examples on fileUploadField available in google or on wicket site has below code snippet. Few things are not clarified here for me.
2.1. What is the getUploadFolder(). Is it the path for source file? or Is it the path for destination? I am allowing user to upload files from mobile device. So, if it is source path, how does it work in my case?
2.1.2. Also, I don't want to save the file either in my local disk or server side. I want the file data to be converted to byte[] and send across to downstream for further processing. Can I do that with fileUploadField or MultiFileUpload?
for (FileUpload upload : uploads)
{
// Create a new file
File newFile = new File(getUploadFolder(), upload.getClientFileName());
// Check new file, delete if it already existed
checkFileExists(newFile);
try
{
// Save to new file
newFile.createNewFile();
upload.writeTo(newFile);
UploadPage.this.info("saved file: " + upload.getClientFileName());
}
catch (Exception e)
{
throw new IllegalStateException("Unable to write file", e);
}
}

You probably already have looked around yourself, but have you seen the Wicket guide?
http://wicket.apache.org/guide/guide/chapter11.html#chapter11_7

Related

SAPUI5 Fileuploader upload not triggered if file to upload is set via setValue

In SAPUI5 I am using fileuploader to to upload file to gateway service.
Functionality works if I select file using by pressing "Browse" button, however if I set file path using using setValue() and after that call upload() function, file upload is not triggered. There are no errors in the browser.
This is the same behavior if I press upload button, but file path is not set. I suspect that there something additional that I need to set, but I have no idea what is that. Below is the code
UI
<u:FileUploader id="fileUploader"
tooltip="Select file to upload"
width="415px"
icon="sap-icon://add-document"
uploadOnChange="false"
sendXHR="true"
uploadUrl="/sap/opu/odata/sap/ZUPLOAD_DOC_SRV/Upload_DocSet"
useMultipart="false"
uploadStart="onUploadStart"
uploadComplete="handleUploadComplete"
sameFilenameAllowed="true"
change="onChange"/>
Controller
var oFileUploader = this.byId("fileUploader");
oFileUploader.removeAllHeaderParameters(oCustomerHeaderToken);
oFileUploader.setValue("C:\\TEMP\\test.txt");
oCustomerHeaderToken = new sap.ui.unified.FileUploaderParameter({
name: "x-csrf-token",
value: oFileUploader.getModel().getSecurityToken()
});
oFileUploader.addHeaderParameter(oCustomerHeaderToken);
var oSlug = new sap.ui.unified.FileUploaderParameter({
name: "slug",
value: slugTemp
});
oFileUploader.addHeaderParameter(oSlug);
oFileUploader.upload();
Can someone help me with this?
For security reasons this is not allowed in JavaScript by Browsers. This has nothing to do with UI5, it is a general security measure.
If it was allowed a script could access ALL your files by generating random paths, read them and upload them to ANY server.
In some browsers it's possible to access a sandboxed environment but it's unlikely that this will help you.
Accessing the complete hard drive is impossible (at the moment).

Open images from Windows store application

I have created a windows store application and I want the user to open Documents, Excel files and picture from the app. I want the files to open in their default application. i.e. Docs in word and pictures in windows picture viewer.
I have used the following code:
FileOpenPicker openPicker = new FileOpenPicker();
openPicker.FileTypeFilter.Add(".Doc");
openPicker.FileTypeFilter.Add(".Docx");
openPicker.FileTypeFilter.Add(".png");
openPicker.FileTypeFilter.Add(".jpg");
StorageFile file = await openPicker.PickSingleFileAsync();
if (file!=null)
{
await Windows.System.Launcher.LaunchFileAsync(file);
}
When I run this and browse to a word document the file opens up fine using word, great.
But if I browse to an image file, it doesn't do anything. I don't get any errors.
Any ideas what I need to do?
Thanks
There is no error and, simply, nothing happens? That's strange.
Here's my go-to syntax, but it's basically yours:
Let's consider what should happen. When you "launch" a docx you are basically asking the default viewer to open for that file, in that case Word. With an image you are asking the image viewer to launch. Should it work? Yes.
Launching a docx when Word is not installed should not result in the behavior your are seeing. No. You should get prompted to find a viewer in the Store. Same with an image. Even without a viewer you should get something.
Not to be a dork here, but have you rebooted? It really sounds more like something strange has happened. You might also try appending "file:///" to the front of the URL to perhaps invoke the file viewer more explicitly. None of that should be necessary however.
Best of luck
One other thing you can do is to force app picker if default program could not be launched like following:
if (file != null)
{
var options = new Windows.System.LauncherOptions();
options.DisplayApplicationPicker = true;
bool success = await Windows.System.Launcher.LaunchFileAsync(file, options);
}

DiskCache non-image files without checking directory

I'm using ImageResizer to resize profile pictures (uploaded by users) on the fly. The profile pictures are stored with the filename being their ID, and without an extension. They are also in a directory that contains other files uploaded by users that may not be images. I reference the file in my cshtml file using something like this:
<img src="//localhost:34754/8FF479AC7724A82A001603EF1566A7EFD852D35561BC2E14B1A01D99CEC035C7F265A3FF7F2642098DD2999217EC7A94?width=100" />
I want ImageResizer to know not to ignore this request, even though it doesn't have an image extension. I found this page: http://imageresizing.net/docs/howto/cache-non-images
This describes how to solve an issue similar to mine, except it requires specifying a directory. But I don't want ImageResizer to handle all requests from this directory, since there are non-images stored there as well. Is there some other way I can tell ImageResizer not to ignore the URL? Maybe by adding something to the query string? Thanks for your help.
The sample event handler uses a directory and a file extension as an example; you are free to use a querystring key or any other kind of indicator as you wish; it's just an example.
Config.Current.Pipeline.PostAuthorizeRequestStart += delegate(IHttpModule sender2, HttpContext context) {
var query = Config.Current.Pipeline.ModifiedQueryString;
if (query["flag"] != "true") return;
Config.Current.Pipeline.SkipFileTypeCheck = true; //Skip the file extension check.
//Non-images will be served as-is
//Cache all file types, whether they are processed or not.
Config.Current.Pipeline.ModifiedQueryString["cache"] = ServerCacheMode.Always.ToString();
};

Meteor upload empty files to public folder

I'm trying to upload files in Meteor using this great script. I modified the event to handle multiple files, like this:
'click #saver': function(ev) {
$.each( $(".fileuploader"), function (index, item) {
if(item.files.length > 0) {
Meteor.saveFile(item.files[0], item.files[0].name);
}
})
}
Everything else is exactly the same as in the Gist (see link to script, above).
The upload shows no errors and the page reloads after the public folder is changed, but most of the files uploaded to the public folder show up as empty, (i.e. they are 0kb in size). There seems to be no pattern. Sometimes all files are empty, sometimes only a couple, and in no predictable order. The console sometimes logs correctly, and other times doesn't. Any thoughts?
Thanks, as always, for your considered advice.
db
It's not that easy at this moment. Files in public dir are managed by Meteor. Therefore, whenever contents of that directory change, the server reloads itself - terminating the file save you've been inside.
The solution is to put files in a place Meteor does not care about: hidden folder (.name), ignored folder (name~), or folder outside of Meteor directory.
Then you'll need to serve those files by hand. See this answer for a snippet:
Dynamically insert files into meteor public folder without hiding it

How to best to cancel a Zend Form File Upload?

I am experimenting with uploading files in a Zend_Form using the Zend_Form_Element_File doing something like this in my form so that the actual upload can be handled by my controller (which in my case is to eventually do some custom file re-naming on the fly).
$element = new Zend_Form_Element_File('Upload');
$element->setDestination(UPLOAD_PATH)
->setValueDisabled(true);
$submit = new Zend_Form_Element_Submit('submit');
In the controller action I then have (very simplified) code like the following which works well:
$form = new MyForm();
$this->view->form = $form;
if ($this->getRequest()->isPost()) {
$formData = $this->getRequest()->getPost();
if ($form->isValid($formData)) {
$form->Upload->receive();
$this->_redirect('/nextAction');
} else {
$form->populate($formData);
}
}
However (and here it comes) what I want to do is have a cancel button on my form handled like this:
[Form]
$cancel = new Zend_Form_Element_Submit('cancel');
[Controller]
if ($this->getRequest()->isPost()) {
$formData = $this->getRequest()->getPost();
$cancel = $this->getRequest()->getPost('cancel');
if ($cancel == 'Cancel') {
$this->_redirect('/nextAction');
}
if ($form->isValid($formData)) {
$form->Upload->receive();
$this->_redirect('/nextAction');
} else {
$form->populate($formData);
}
}
The trouble starts when the user selects a file via 'Upload' and then hits 'cancel' to exit out of the action. The form doesn't know the difference between 'Submit' and 'Cancel' and initiates the transfer from the browsers end. On the server end the receive() function isn't called and that hangs the entire process.
In this older (Non-Zend) SO question "Cancel a webform submit with php" the suggested answer is to have the cancel button in a separate form. I don't think that this is what I want to do in a Zend based system.
In this old (Zend) forum question "Zend Form Element File upload issues" the suggested answer for canceling an upload is to do something like this:
if ($cancel == 'Cancel') {
unlink($form->Upload->getValue());
$this->_redirect('/nextAction');
}
I have tried this and it seems to work, but I don't know why and when I look behind the curtains I actually see that it generates an error of the form:
Warning: unlink(<filename>): No such file or directory in <line in controller code where unlink() call is>
So my big question is what is the best way to be canceling out of the file upload? (and what does the unlink do - even with the error?)
Edit 3/3
To clarify, HTML form uploads are pretty dumb. If you have multiple submit buttons on a form that has a File element, then no matter what submit button you seem to use the file gets transferred to the temp upload directory on your server.
But Zend tries to be clever, it allows you to defer copying the file from the temp directory to the final destination through "setValueDisabled(true)" in the form and "receive()" in the controller.
However if you initiate an upload in the form, but do not call "receive() in the controller, then the entire Zend process seems to lock up (I get the browser continually announcing "loading" and doing nothing else).
I am looking for a Zend (or pure php) based solution to back out of the file upload on the server side, after the file has arrived in the temp directory, but before a call to "receive()" is required.
The "unlink" method seems to work, but it also throws a warning and as I have no real idea of why it works I am suspicious of what is actually going on.
Try if ($cancel->isChecked()) instead of if ($cancel == 'Cancel')
EDIT
This is how file uploads work. The form is submitted, and the browser sends the file, through HTTP to server. PHP sees a form with enctype "multipart/form-data" and a file element, and saves that file in a temporary location. At this point, if you call "Receive", it actually calls, behind the scenes, the PHP functions which move that file out of its temporary location.
Once you hit submit, the browser sends the file, whether you like it or not. You can choose to ignore the file, which is what it looks like you're trying to do. However, if ($cancel == 'Cancel'), doesn't look it will work because the value is case-sensitive. To avoid error-prone code like this, if ($cancel->isChecked()) is preferred.
Can you be a little more specific on what exactly you're trying to do?