-
FEATURED COMPONENTS
First time here? Check out the FAQ!
Orignial message at:
https://sourceforge.net/forum/message.php?msg_id=4228132
By: soniks
Hi,
I am trying to upload a text file using the upload facility but it always seems to indicate that I am uploading a binary file. How does zk determine whether the file being uploaded is text or binary? Here is the code based on the examples.
I used a simple text file that I created in Notepad.
<window id="imgwnd" title="fileupload demo" border="normal">
<button label="Upload">
<attribute name="onClick">{
import org.zkoss.util.media.Media;
Object media = Fileupload.get();
if (media instanceof org.zkoss.image.Image) {
Image image = new Image();
image.setContent(media);
image.setParent(imgwnd);
} else if (media instanceof Media) {
Media file = (Media)media;
if (file.isBinary())
alert("Binary file");
else
alert("Text file");
alert(file.getFormat());
alert(file.getContentType());
}
}</attribute>
</button>
<separator/>
</window>
TIA
Orignial message at:
https://sourceforge.net/forum/message.php?msg_id=4228802
By: chipocle
try
//Object media = Fileupload.get();
Media media = Fileupload.get();
next line only for work *.txt
tempMedia= new AMedia(media.getName(),"txt","text/plain",media.getStreamData());
(Media)tempMedia;
Orignial message at:
https://sourceforge.net/forum/message.php?msg_id=4231571
By: soniks
I tried the following but it still indicates a binary file.
<window id="imgwnd" title="fileupload demo" border="normal">
<button label="Upload">
<attribute name="onClick">{
import org.zkoss.util.media.Media;
import org.zkoss.util.media.AMedia;
Media media = Fileupload.get();
if (media instanceof org.zkoss.image.Image) {
Image image = new Image();
image.setContent(media);
image.setParent(imgwnd);
} else if (media instanceof Media) {
AMedia tempMedia = new AMedia(media.getName(),"txt","text/plain",media.getStreamData());
if (tempMedia.isBinary())
alert("Binary file");
else
alert("Text file");
alert(tempMedia.getFormat());
alert(tempMedia.getContentType());
}
}</attribute>
</button>
<separator/>
</window>
Orignial message at:
https://sourceforge.net/forum/message.php?msg_id=4232706
By: robbiecheng
add toString() just after media.getStreamData.
/robbie
Orignial message at:
https://sourceforge.net/forum/message.php?msg_id=4233855
By: soniks
This solved the first problem but does not fix the underlying problem which is that the Fileupload.get() returns a byte stream media object rather than a text based one.
I tried the following in the ZK Demo and there are still errors when trying to use the getReaderData() function :
<window id="imgwnd" title="fileupload demo" border="normal">
<button label="Upload">
<attribute name="onClick">{
import org.zkoss.util.media.Media;
import org.zkoss.util.media.AMedia;
import java.io.BufferedReader;
import java.lang.StringBuffer;
Media media = Fileupload.get();
if (media instanceof org.zkoss.image.Image) {
Image image = new Image();
image.setContent(media);
image.setParent(imgwnd);
} else if (media instanceof Media) {
AMedia tempMedia = new AMedia(media.getName(),"txt","text/plain",media.getStreamData().toString());
if (tempMedia.isBinary())
alert("Binary file");
else
alert("Text file");
alert(tempMedia.getFormat());
alert(tempMedia.getContentType());
BufferedReader r = null;
try {
r = new BufferedReader(tempMedia.getReaderData());
String line = null;
StringBuffer buf = new StringBuffer();
while ((line = r.readLine()) != null) {
buf.append(line);
}
alert(buf.toString());
}
catch(IOException e) {
alert("Error " + e.getMessage());
}
finally {
if (r!=null) {
r.close();
}
}
}
}</attribute>
</button>
<separator/>
</window>
Orignial message at:
https://sourceforge.net/forum/message.php?msg_id=4239128
By: henrichen
Please post to Bug list. Thanks.
/henri
Orignial message at:
https://sourceforge.net/forum/message.php?msg_id=4239157
By: fenixartzz
this worked for me am able to upload any file
Object media = Fileupload.get();
String name = "//home//" + userid.getValue() + "//"+ media.getName(); InputStream inputf = media.getStreamData(); int size = inputf.available(); OutputStream f = new FileOutputStream(name); byte [] buf = new byte[size]; inputf.read(buf); f.write(buf); f.close(); inputf.close();
Asked: 2007-03-26 11:38:13 +0800
Seen: 1,203 times
Last updated: Apr 02 '07