cancel
Showing results for 
Search instead for 
Did you mean: 

Uploading and downloading files

Former Member
0 Kudos

Hi,

I am running TutWD_File_UpDownload project given by SAP.When I uploaded file,it is showing sucessfully uploading message,but when i am searching that file

in WAS server , that file is not exist in server.So how i will get that file in server .

Thanks & Regards

muna

Accepted Solutions (1)

Accepted Solutions (1)

Sigiswald
Contributor
0 Kudos

Hi muna,

If the file is successfully uploaded, it's content is stored in the Web Dynpro context. If you want to save it to disk, you could add the following code to the onActionUploadFile method.


//IWDModifiableBinaryType binaryType =
//byte[] file =

writeByteArrayToFile(new File("/tmp/" + binaryType.getFileName()), file);

The next code is copied from <a href="http://jakarta.apache.org/commons/io/">Commons IO's FileUtils</a>


    public static FileOutputStream openOutputStream(File file) throws IOException {
        if (file.exists()) {
            if (file.isDirectory()) {
                throw new IOException("File '" + file + "' exists but is a directory");
            }
            if (file.canWrite() == false) {
                throw new IOException("File '" + file + "' cannot be written to");
            }
        } else {
            File parent = file.getParentFile();
            if (parent != null && parent.exists() == false) {
                if (parent.mkdirs() == false) {
                    throw new IOException("File '" + file + "' could not be created");
                }
            }
        }
        return new FileOutputStream(file);
    }

    public static void writeByteArrayToFile(File file, byte[] data) throws IOException {
        OutputStream out = null;
        try {
            out = openOutputStream(file);
            out.write(data);
        } finally {
            closeQuietly(out);
        }
    }

    public static void closeQuietly(OutputStream output) {
        try {
            if (output != null) {
                output.close();
            }
        } catch (IOException ioe) {
            // ignore
        }
    }

Kind regards,

Sigiswald

Answers (6)

Answers (6)

Former Member
0 Kudos

Hi Sigiswald and Siddharth,

Thank to both.My problem is solved because of your guidance.

Thanks & Regards

muna

Former Member
0 Kudos

Hi Sigiswardl,

Thank you very much for your reply.My problem is using your code ,pdf file is created in server ,but when i am trying to see contents of pdf file in server side only blank pages are there (no data available in that file),but in my local system which pdf file i uploaded contains data (no blank page is there).In this discussion the code i used for downloading files from server side is not working ,kindly tell me where is exact problem in code. which server path i will give for downloading.All the files are uploaded into \tmp folder ,so IS path should be \tmp or

c:\temp.

Thanks & Regards

muna

sid_sunny
Contributor
0 Kudos

Hi Muna,

Instead of writing the file to the default location try writing it on a specific location like c:\abc\<your file name> and then see whether you are able to write the file and is the file getting wrote with data or not.

Use the same path while download too. I have used this code and it worked for me so it should work for you too.

Regards

Sid

Sigiswald
Contributor
0 Kudos

Hi muna,

If I understand it correctly, you upload a file from your local computer to the server using a Web Dynpro FileUpload UI element. Once the file is uploaded, you write it to disk on the server using the/similar code I mentioned in my first post. However, pdf files are not correctly stored on the server.

1. is the file actually created on the server, i.e. can you see it?

2. is the size of the created file correct, at least greater than 0 bytes?

3. if you upload a zip file, can you unzip it on the server?

4. if you upload a pdf file, is the file size of the created file on disk greater than 0 bytes?

The point is I really can't believe it works fine with any file except a pdf file. I can very well imagine it doesn't work at all, e.g. writing to an unexisting directory or not having write privileges on the file system of the server. In which case an IOException should be thrown.

The 2nd question about reading files from the server and downloading them to the client can be looked upon later. After all, if the upload doesn't work, the download won't work either.

Kind regards,

Sigiswald

Sigiswald
Contributor
0 Kudos

OK, you were 2 minutes faster

Good luck!

Former Member
0 Kudos

Hi Siddharth,

The below code is not working for download pdf file from server to local system

IWDAttributeInfo attInfo = wdContext.currentContextElement().node().getNodeInfo().getAttribute("PdfSource");

System.err.println("with in init1");

ISimpleTypeModifiable type = attInfo.getModifiableSimpleType();

System.err.println("with in init2");

IWDModifiableBinaryType binaryType = (IWDModifiableBinaryType) type;

System.err.println("with in init3");

binaryType.setFileName("EP and WD.pdf"); // Set file name

System.err.println("with in init4");

binaryType.setMimeType(WDWebResourceType.PDF); // set the mime type

System.err.println("with in init5");

String fileName = "/tmp/EP and WD.pdf";

ByteArrayOutputStream out=null;

try{

System.err.println("with in try");

File file = new File(fileName);

FileInputStream in = new FileInputStream(file);

out = new ByteArrayOutputStream();

int length;

byte[] part = new byte [10 * 1024];

while ((length = in.read(part)) != -1)

{

out.write(part, 0, length);

}

in.close();

out.close();

System.err.println("with in try end of close");

}

catch(IOException i){i.printStackTrace();}

wdContext.currentContextElement().

setPdfSource(out.toByteArray());

Thanks & Regards

muna

sid_sunny
Contributor
0 Kudos

Hi Muna,

Can you please specify what the exception you are facing?

For a more clear usage of file upload download of a PDF go through the following link: <a href="https://www.sdn.sap.comhttp://www.sdn.sap.comhttp://www.sdn.sap.com/irj/servlet/prt/portal/prtroot/com.sap.km.cm.docs/library/webdynpro/offline%20interactive%20pdf%20form%20using%20download.pdf">link</a>

Regards

Sid

Former Member
0 Kudos

Hi Siddharth,

Now i am only doing uploading file.I have no idea how to download file from server.May be Sigiswald can give some solution, that is next requirement for me also.If you will get any solution please share it with me also.

Thanks & Regards

muna

sid_sunny
Contributor
0 Kudos

Hi Muna,

File Download is vey simple. Just insert this code in your wdDoInit:

IWDAttributeInfo attInfo =

wdContext.currentContextElement().node().getNodeInfo().

getAttribute("PdfSource");

ISimpleTypeModifiable type = attInfo.getModifiableSimpleType();

IWDModifiableBinaryType binaryType = (IWDModifiableBinaryType) type;

binaryType.setFileName("your file name.pdf"); // Set file name

binaryType.setMimeType(WDWebResourceType.PDF); // set the mime type

String fileName = <path of the file on server>

File file = new File(fileName);

FileInputStream in = new FileInputStream(file);

ByteArrayOutputStream out = new ByteArrayOutputStream();

int length;

byte[] part = new byte [10 * 1024];

while ((length = in.read(part)) != -1)

{

out.write(part, 0, length);

}

in.close();

wdContext.currentContextElement().

setPdfSource(out.toByteArray());

PdfSource here is to be bind with <data> property of the FileDownload Functionality. For more information you can select SHOW HELP in the context menu of you FileDownload UI element.

Do Reward Points if it helps.

Regards

Sid

Former Member
0 Kudos

Hi Siddharth,

The above code is doing same thing what you are suggesting.I already tested your code same result is comming.

Thanks & Regards

muna

sid_sunny
Contributor
0 Kudos

Hi Muna,

How you are viewing the saved file again are you using the FileDownload UI Element or the Interactive Form UI Element. Please Specify.

Regards

Sid

Former Member
0 Kudos

Hi Sigiswald,

Thanks for your immediate help .Using your code now i able to get the file in server which i uploaded but for pdf files it is uploading in server but pdf file's contents are missing in the file.So how i will get contents of pdf file.

Thanks & Regards

muna

sid_sunny
Contributor
0 Kudos

Hi Muna,

Try out this

File f1 = new File(<Location where you want to save your file on server followed by the file name>);

FileOutputStream out = null;

try {

out = new FileOutputStream(f2);

out.write(<Pdf Source context>);

} catch (IOException e) {

e.printStackTrace();

}

using this I am able to see the data of the Pdf too. Do reward points if it helps.

Regards

Sid

Sigiswald
Contributor
0 Kudos

Hi muna,

What exactly do you mean? You're using a FileUpload UI element, right? That allows you to upload any kind of file, including pdf files. The code I posted just saves that particular file to disk, no matter what kind of file it was. It should work with pdf files as well. What do you mean by "pdf file's contents are missing"?

Your 2nd requirement is to read some file, previously uploaded or already present, from disk and allow any user to download it using a FileDownlaod UI element?

Kind regards,

Sigiswald