cancel
Showing results for 
Search instead for 
Did you mean: 

Upload a file

Former Member
0 Kudos

Hi, I want to upload a file. I have search in the forum and tried the different solutions.

Those are the steps I have following:

- in the context I have create a node with an attribute FileName (String) and FileData (binary).

- in the properties of the FileUpload's UIElement: data->File.FileData, fileName -> File.FileName

- in the wdInit():

IWDAttributeInfo attInfo = wdContext.nodeFile().getNodeInfo().getAttribute("FileData");

ISimpleTypeModifiable type = attInfo.getModifiableSimpleType();

IWDModifiableBinaryType binaryType = (IWDModifiableBinaryType) type;

- In the button associated to the upload:

try {

String location = "C://";

String file = wdContext.currentFileElement().getFileName();

String fileName = location +file;

File dest_file = new File(location,file);

FileOutputStream out = new FileOutputStream(dest_file);

if(wdContext.currentFileElement().getFileData() != null)

out.write(wdContext.currentFileElement().getFileData());

else

wdComponentAPI.getMessageManager().reportSuccess("The data is null");

out.close();

}

catch (FileNotFoundException e) {

wdComponentAPI.getMessageManager().reportSuccess("The Error"+e.getMessage());

}

catch (IOException e) {

wdComponentAPI.getMessageManager().reportSuccess("The Error"+e.getMessage());

}

When I deploy the application I get the following error:

The ErrorC:\work\pass red.txt (The system cannot find the path specified)

While debuging I have find out that the error is in the following code line:

FileOutputStream out = new FileOutputStream(dest_file);

, but I don't find the error or what is wrong with the code.

Would any of you help me?

Thanks

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi,

I think one more "\" is required in the file path.

Ex : "C:\work\pass\red.txt "

Regards, Anilkumar

Answers (7)

Answers (7)

Former Member
0 Kudos

Resolved

Former Member
0 Kudos

Hello again,

no, it didn't ends with exception, it's all ok, but it didn't create the directory.

Former Member
0 Kudos

Hi,

Refer this link

https://www.sdn.sap.com/irj/sdn/developerareas/webdynpro?rid=/library/uuid/49f2ea90-0201-0010-ce8e-d...

follow the tutorial

File Upload/Download 23 | 30

Regards,

RK

Former Member
0 Kudos

Hi Valery, thanks for your answer.

I have tried with your code but it doesn't do anything... I have no idea why.

Thanks anyway.

Former Member
0 Kudos

Mireia,

1. Did it fails with exception? What is stack trace?

2. If [1] is ok, did it create necessary directories?

3. If [2] is ok, did it create file?

4. If [3] is ok, did it populate file with static content (rather then uploaded file)?

VS

Former Member
0 Kudos

Mirera,

FileOutputStream constructor may fail if:

1. There is a security restrictions for accessing file system (hardly to be a case here)

2. There is a directory with the same name as file on the path

3. Some of directories on the path do not exist.

So, I guess your case is 3. So try the following code to write a file:


public static void create(final String name, final byte[] content) throws IOException
{
  final String theName = name.replace('\', '/');
  final int lastSeparator = theName.lastIndexOf( '/' );
  final File dir;
  final File file;
  if ( lastSeparator > 0 )
  {
    dir  = new File( theName.substring(0, lastSeparator) );
    file = new File( dir, theName.substring( lastSeparator + 1) );
  }
  else
  {
    dir  = new File( "." );
    file = new File( dir, theName.substring( 0 == lastSeparator ? 1 : 0 ) );
  }
  if ( !dir.exists() ) dir.mkdirs();
  final FileOutputStream fos = new FileOutputStream( file );
  try
  {
    fos.write( content );
  }
  finally
  {
    try { fos.close(); } catch (final IOException ex) {}
  }
}

Invoke it as:


final byte[] content =
  {'H','e','l','l','o',',',' ','W','o','r','l','d','!'};
create("C:\some-x\Some File.txt", content);

VS

Former Member
0 Kudos

helo Mireia

instead of

String location = "C://";

write

String location = "C:
";

i think this should work.

Regards,

Piyush.

Former Member
0 Kudos

Hi,

thanks all for your answers. It seems (I don't know why) that I can only put in the path the root of the local directory (without folders).

Also, although I haven't any errors while deploying the application, I don't get a copy of my upload file as a result... any suggestion would be highly appreciate.

Former Member
0 Kudos

hi

the loc should be given as "c:
filename.doc". Other than this nothing seems to be wrong.

regards

vln

Former Member
0 Kudos

Hi Mireia

one of the the options, try with the following path

C:

work
pass
red.txt

Regards

Pran

Message was edited by: Pran Bhas