Important Notice - Forums is archived
To simplify things and help our users to be more productive, we have archived the current forum and focus our efforts on helping developers on Stack Overflow. You can post new questions on Stack Overflow or join our Discord channel.

Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.
File Upload Fails when no file selected
Hi,
Here is my code, which fires an Upload failed error, when I click on the Upload button without selecting any files. I know it is because of returning null value, but instead of this null I tried "return new ByteArrayOutputStream();" , still it shows Upload failed error.
I also tried to disable upload button using addChangeListener, but it disables the upload button only for first time.
How can I fix these issues, can anybody help me, Thanks in advance.!!!!!!!!!
private class ImageUploader implements Receiver, SucceededListener
{
private static final long serialVersionUID = -878399743277824104L;
public File file;
@Override
public OutputStream receiveUpload(String filename, String mimeType)
{
FileOutputStream fos = null; // Stream to write to
try
{
String tmpFolderLoc = getLocalFileLocation();
// Open the file for writing.
file = new File(tmpFolderLoc + File.separatorChar + filename);
fos = new FileOutputStream(file);
} catch (final java.io.FileNotFoundException e)
{
new Notification("Could not open file<br/>", e.getMessage(), Notification.Type.ERROR_MESSAGE).show(Page.getCurrent());
return new ByteArrayOutputStream();
}
return fos; // Return the output stream to write to
}
@Override
public void uploadSucceeded(SucceededEvent event)
{
attachCall.afterAttachementDone(file);
// attachUpload.setButtonCaption(null);
// refresh();
}
}
You should try new NullOutputStream() instead of ByteArrayOutputStream()
hope it works :)