• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to get value of input type file in javascript

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,


i need to get the value of input type file, i.e. selected from local machine to variable... its not giving me the error but its not running it.

following is my code i am running


<%@page import=" javax.servlet.ServletRequest" %>
<html>
<head>
<title>JSP Page</title>
<script type="text/javascript">
function ViewImage()
{
var userInput = document.getElementById('userInput');

// 1). I tried using following one :
var filename= document.getElementById(browse);
imageview.innerHTML += '

hello

' + filename.value + '

';


var imageview = document.getElementById('ImageView');
imageview.innerHTML += '

' + userInput.value + '

';

/* 2). Also tried using this one too.
var files = $'input[type="File"]';
imageview.innerHTML += '

hello

' + files.value + '

';*/

/* 3). tried using this too
String filename= request.getParameter(browse);
imageview.innerHTML += '

hello

' + filename.value + '

';
*/
}
</script>
</head>
<body>
<h1>Hello World!</h1>


<div id="ImageView"></div>

<input type="text" name="userInput" id="userInput"/>

File needed to upload
<input type="File" name="Browse" id="browse" onclick="ViewImage();" >

<!-- <button type="button" name="submit" onclick="ViewImage();"> Submit
</button> -->

</body>
</html>

i tried all the possibilties that i can find on internet. But i didnt get the output of it. but its shows output if i enable the button tag. please suggest the solution. i want the whole path and name of file uploading.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For security reasons this information is not available. What are you actually trying to accomplish?
 
nanu shah
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Bear Bibeault,

i apology for this... I tried to search for javascripts but could not find it. So and i was thinking to implement this thing using jsp that why i posted over here...

Now actually i want to display the doc and pdf file on same webpage... I was thinking that if i can get the filename from local machine that i can covert it into jpg format to display it on same webpage.

if you can suggest me any other alternative, i would be thankful to you...

Thanks
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is your variable browse?



Eric
 
nanu shah
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Eric

Browse is the id of the input type "File"
 
Eric Pascarello
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look at it more closely...

There is an error there.

Eric
 
nanu shah
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Eric Pascarello and Bear Bibeault
For your instant reply and guiding me.... I have found the resolution to get the file name through the Javascript, its the following code...


<%@page import=" javax.servlet.ServletRequest" %>
<html>
<head>
<title>JSP Page</title>
<script type="text/javascript">
function ViewImage(filename)
{

var imageview = document.getElementById('ImageView');
imageview.innerHTML += '

' +filename.value+'

';

}
</script>
</head>
<body>
<h1>Hello World!</h1>


<div id="ImageView"></div>
File needed to upload
<input type="File" name="Browse" id="browse" onchange="ViewImage(this);" >

</body>
</html>


But Now i have one more problem... i want to write a javascript in a way that i can view the word document and pdf document on same webpage.... So please guide me ahead or suggest me what need to do ahead...?


i am using following code but its opening in microsoft word and also not displaying me the document.
<script type="text/javascript">
function ViewImage(filename)
{
var imageview = document.getElementById('ImageView');
imageview.innerHTML += '

' +filename.value+'

';
openWordDocPath(filename);
}

function openWordDocPath(strLocation)
{
var objWord;

var imageview = document.getElementById('ImageView');
imageview.innerHTML += '

' +strLocation+'

';
objWord = new ActiveXObject("Word.Application");
objWord.Visible = true;

var imageview = document.getElementById('ImageView');
imageview.innerHTML += '

' +strLocation+'

';
objWord.Documents.Open(strLocation);

}
</script>

please help me...

thanks
 
reply
    Bookmark Topic Watch Topic
  • New Topic