0

Upload of text file

asked 2007-03-26 11:38:13 +0800

admin gravatar image admin
18691 1 10 130
ZK Team


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



delete flag offensive retag edit

6 Replies

Sort by ยป oldest newest

answered 2007-03-26 18:45:41 +0800

admin gravatar image admin
18691 1 10 130
ZK Team


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;

link publish delete flag offensive edit

answered 2007-03-28 12:57:35 +0800

admin gravatar image admin
18691 1 10 130
ZK Team


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>

link publish delete flag offensive edit

answered 2007-03-28 16:58:18 +0800

admin gravatar image admin
18691 1 10 130
ZK Team


Orignial message at:
https://sourceforge.net/forum/message.php?msg_id=4232706

By: robbiecheng

add toString() just after media.getStreamData.

/robbie

link publish delete flag offensive edit

answered 2007-03-29 10:20:19 +0800

admin gravatar image admin
18691 1 10 130
ZK Team


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>

link publish delete flag offensive edit

answered 2007-04-02 06:57:44 +0800

admin gravatar image admin
18691 1 10 130
ZK Team


Orignial message at:
https://sourceforge.net/forum/message.php?msg_id=4239128

By: henrichen

Please post to Bug list. Thanks.

/henri

link publish delete flag offensive edit

answered 2007-04-02 07:34:17 +0800

admin gravatar image admin
18691 1 10 130
ZK Team


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();


link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow

RSS

Stats

Asked: 2007-03-26 11:38:13 +0800

Seen: 1,203 times

Last updated: Apr 02 '07

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More