0

I'm facing a simple problem I would like to get a local path from the end-user of my web application. I need only that and not to actually upload a file. (I know there are fileUpload tags in seam or in Primefaces but I just need the local full path as I'm uploading directly to Picasa Web Albums via the Google API)

In other words I would like to bind some kind of html tag : input type="file" to a bean property (I notice that the JSF tag h:inputText tag doesn't have a type attribute) Any ideas?

Env : JBoss AS 5.1, JSF1.2, Seam 2.2, Primefaces 1.1

Edit : here is my working solution

Thanks to the answers, I implemented the use-case of uploading a file directly to Picasa

    <h:form prependId="false" enctype="multipart/form-data">
        <s:fileUpload id="fileUpload"
                data="#{picasa.incomingFile}"
                contentType="#{picasa.fileType}"/>
        <h:inputText id="albumId"
                value="#{picasa.albumId}" />
        <h:commandLink action="#{picasa.upload()}"
                value="Upload">
            <f:param name="s"
                    value="#{subjectHome.id}"/>
        </h:commandLink>
    </h:form>

and the component code

@Name("picasa")
public class PicasaService {

    @Logger private Log log;

    private PicasawebService service;
    private InputStream incomingFile;
    private String fileType;
    private String albumId;



    @Create
    public void setUp()
    {
        service = new PicasawebService("picasaService");
        try {
            service.setUserCredentials("xxx@yyyy.zzz", "password");
        } catch (AuthenticationException e) {
            e.printStackTrace();
        }
    }

    public void upload()
    {
        URL albumUrl;
        PhotoEntry returnedPhoto;
        try {
            albumUrl = new URL("https://picasaweb.google.com/data/feed/api/user/default/albumid/" + albumId);
            MediaStreamSource myMedia = new MediaStreamSource(incomingFile , this.fileType);
            returnedPhoto = service.insert(albumUrl, PhotoEntry.class, myMedia);
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ServiceException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

This way, it seems to me that the file is not transferred twice (one from end-user machine to my web app's server and from there to picasa WA server).

Hope this helps

koyaga
  • 1,005
  • 1
  • 9
  • 14

2 Answers2

1

You can't get the local path for security reasons, all modern browsers won't provide your code with that information... bestcase is they give you the file name only...

Yahia
  • 69,653
  • 9
  • 115
  • 144
  • Thank you for your answer. Maybe I don't understand : for example the html tag actually provides a local path. I only need to bind that value to a JSF bean. – koyaga Nov 05 '11 at 16:07
  • Even if not exactly the same, this question is related to http://stackoverflow.com/q/7942611/585104 – koyaga Nov 05 '11 at 16:33
  • 1
    @Koyaga: did you read and understand the answer? There is no way. Period. Some poor browsers passes the full path along the filename, but it's the browser's mistake to do so. – BalusC Nov 05 '11 at 16:34
  • @koyaga No - that works only if security are extremely relaxed and/or used with very old browsers and/or with Flash/Silverlight... in pure client-side HTML/JS this is not possible - see for example https://developer.mozilla.org/en/Using_files_from_web_applications... for JSF-handling of your scenario see the answer from BalusC – Yahia Nov 05 '11 at 16:42
1

but I just need the local full path as I'm uploading directly to Picasa Web Albums via the Google API

You need to get the file's content as an InputStream from the JSF file upload component and write it to an FileOutputStream on the server's local disk file system the usual way, then you can reference it as a File and pass it to the Picasa API as documented.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thank you! Seems to be the best answer for my problem. I suppose there is no way in my scenario not to transfer the file twice. – koyaga Nov 05 '11 at 16:51
  • 1
    You cannot transfer data directly from client X to client Y by plain HTML. You have to play for the mediator yourself. In any case, the client has **already** sent the file's contents when using ``, so why would you ignore it? – BalusC Nov 05 '11 at 16:55