File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes HTML, CSS and JavaScript and the fly likes attaching files through JavaScript Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "attaching files through JavaScript" Watch "attaching files through JavaScript" New topic
Author

attaching files through JavaScript

indu yeturu
Ranch Hand

Joined: Aug 27, 2008
Posts: 123
Hi , Please help me how to attach files like in Gmail-style using Javascript.

Thanks
indu
[ August 29, 2008: Message edited by: Bear Bibeault ]

thanks,
indu
Angel J Gama
Ranch Hand

Joined: Jun 28, 2007
Posts: 36
You can use an iFrame to upload your files. Have you read the link that David O'Meara sent you for uploading files?
If using an iFrame, you should create one form per attachment, and every html-form should be submitted "within" an iframe. How?

... and so on. Once the servlet has uploaded your file, you'll see the response inside the iframe it was sent.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13868
    
  10

We have a separate forum for questions about HTML and JavaScript. I will move this topic to that forum; please follow the link at the top of this page to continue.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Stoyan Stefanov
author
Ranch Hand

Joined: Jul 16, 2008
Posts: 61
YUI's Connection Manager (Ajax) abstracts a bit of hard work for you and makes your file upload code look like a normal Ajax request. It's using an iframe behind the scenes.

Or if you want to go fancy, flickr-style, you can use YUI's uploader control. The cool thing about it is that it let's the user upload multiple files in one selection (as in, you hold CTRL and select several files). It's using Flash. Later WordPress versions are using similar type of flash uploader.


<a href="http://www.thinkinginjavascript.com" target="_blank" rel="nofollow">my OOJS book</a>
indu yeturu
Ranch Hand

Joined: Aug 27, 2008
Posts: 123
I am using this JS function to attach a file, as our team wants it to be without AJAX, then, in what way can I upload the files once they are attached?

function addFileInput()
{
var d = document.createElement("div");
var file = document.createElement("input");
file.setAttribute("type","file");
file.setAttribute("name","attachment"+upload_number);
d.appendChild(file);
document.getElementById("moreUploads").appendChild(d);
upload_number++;
}
thanks
indu
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60046
    
  65

Without using Ajax or an iframe, you'll just need to do a normal form submission. You can;t get away without the server-side code to receive the file. JavaScript cannot upload the file on its own -- it can only initiate the client-side request.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Angel J Gama
Ranch Hand

Joined: Jun 28, 2007
Posts: 36
Using iframes isn't ajax. It's just sending the form in another "window".
YOu could use the same JS function, but instead of append an input file within a div, you can so something like this:
It's probable that I've misspelled some js methods, but that's a way to do it (no ajax).
indu yeturu
Ranch Hand

Joined: Aug 27, 2008
Posts: 123
Thanks a lot to All..
indu
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60046
    
  65

Originally posted by Angel J Gama:
Using iframes isn't ajax

Prior to XMLHttpRequest invisible iframes were used to make "backdoor" requests, and many consider that a form of Ajax. When it comes to file uploads, it's the only way to do it.
 
 
subject: attaching files through JavaScript
 
Similar Threads
Issue with file upload in javascript using spring controller
can we make component reusable in javascript?
Need ur suggestion for API
uploading a file but want to restrict the type of files
how to load HTML in DIV