How to browse and display the image in the same page? - javascript

I've a very small doubt. I've a html tag to browse the file. and as soon as I browse the file from the local server, I want to set that path to the source of image tag. Is it possible to implement?
I've code something like this.
<form name="image" id="image" enctype="multipart/form-data" action="" method="post">
<input type="file" name="img" />
<img src=""></img>
</file>
So as soon as I browse and select the file from the local server,that path should be set to image tag.My intention is to browse the image and display it in that page itself. Any help will be appreciated. Thanks in advance.

Browsers have now allowed you to access the path of a file from the input[type=file] tag for a long time. You can't simply do it like you're trying to. In order to accomplish your goal of showing the file after the user selects it, you actually need to upload it to your server. This typically requires a page-refresh, but there are plenty of javascript libraries that are available in order to make it happen without refreshing the page. Once the file is uploaded, you can "ping back" a URL to view the image, and load that in your image tag.
The jQuery library I've been using recently to do inline file uploads is jQuery File Upload

What you need is the full path to the file on the client side - which is not allowed (see also this thread).
Sorry mate, but without sending the file to the server you can't do anything with it other than getting the file name (not the path), the file type and the file size.

Related

Display the uploaded image in another html page

I want to display the image uploaded in home.html to another page named result.html.
<div class="custom-file">
<input type="file" class="custom-file-input" name="myfile" onchange="readURL(this);">
<label class="custom-file-label" for="customFile" name="myfile">Choose file</label>
</div>
<div style="text-align: center;">
<input type="submit" class="mybtn" onclick="showInput();">
</div>
I want to show the uploaded image with name="myfile" in the result.html page.
How should i do it?
I would personally use a php upload page to upload the image and have one the page you want to show the image on have a php array for all the images that have been uploaded and foreach file with .jpg ect.. echo it in html
Many possibilities. Depends on the scope of your application and requirements.
Option 1 - Use a database
Store the uploaded picture to a database from the first page. The database could be anything - MySql , MSSQL, etc. The second page reads the picture contents from the database.
Option 2 - Use a memory cache
You could simply store the picture in a distributed memory cache like memcached or redis.
Option 3 - Use the file system
For very trivial applications (single web server), use the local file system as a data store. I would not recommend this.
Hope this helps.
If you want to do this without using any back-end languages or server side, yes you can!
There is a tricky way using JavaScript or jQuery:
Create a function to save an uploaded image into your project folder, and save uploaded image src in JS variable.
Create onclick function that get the image src from pre-defined variable.
Also this function will re-direct to your result.html page with the image src as parameter , like \result.html?img=SRC-FROM-FUNCTION.
On the other page, create a function to read this parameter and append an img tag with the src from the url.
However, i recommend using back-end language like PHP or C# to do this, this solution because you are using a simple static pages.
#1 Make div inside home.html <div id="foresult"></div> and store your value inside
#2 Make result.html
#3 Make <div id="result"></div> inside result.html
#4 Use this JQuery script inside result.html to get value from home.html
<script>
$(document).ready(function(){
$('#result').load('home.html #foresult');
});
</script>
Normally this would be done server side but you might be able to upload the file to local storage and then reference it on the next page. This wouldn't necessarily work in all browsers though.
https://stackoverflow.com/a/16505576/5065282
https://developer.mozilla.org/en-US/docs/Web/API/File/Using_files_from_web_applications

Use javascript to change the browse file path

I am learning javascript. Can I use javascript to change the browse file path when input type is file?
<input type="file" id="file_input" name="file_name"/>
When I click the "browse file..." button, the window is my assign path.
I do not hope the user to find the file location from the window. I am sorry my poor English
I don't think this is possible. See here for a more complete explanation, but basically it's a security issue. Code from your web page should not be allowed to know anything about the file structure on a client machine.

How to post a file to a form using javascript automatically

I have some client-side JavaScript which will output a jpeg file based on HTML5 canvas manipulation, if the user performs an action such as clicking the "OK" button.
I would like the jpeg output to be automatically loaded into the "Upload Front Side" field in a form, as if the user uploaded the file from his or her own hard drive.
However, I can't seem to get it to work.
Here is the form:
<div class="property-wrapper">
<label for="upload-front">Upload Front Side</label>
<input id="upload" class="file" type="file" enctype="multipart/form-data" id="Front-Side" name="properties[Front Side]" onchange="readURL(this);" accept="image/*" />
</div>
<script>
document.getElementById('upload').value="http://upload.wikimedia.org/wikipedia/commons/thumb/2/22/Turkish_Van_Cat.jpg/353px-Turkish_Van_Cat.jpg"
</script>
The problem is browsers have several restrictions on what can be done programatically with file upload due to security reasons, have a look at this answer.
The file upload functionality is potentially exploitable, and some browsers will for example not open the explorer box if for example the file upload input field is hidden with display:none.
Most browsers will not allow programatic clicks to a file upload element and require the user to click them instead, to prevent attacks where someone sends a link to a page that immediately steals content from the user's hard drive.
So the functionality you mention does not seem to be feasible due to common browser security restrictions. There are usually no error messages or warnings, it just doesn't work.
An alternative to using the file upload browser input component could be to encode the contents of the file in Base64 and send in the body of an ajax POST for example.
Are you asking how to upload to a server, via a form, the graphic image you extracted from the canvas of your page? This would be useful, I hope to see this answered.
I would start by enabling the user to download/export the image. This might be done with a blob. Ive done this for text data exports, works nicely.
Maybe there is a way to apply the same tricks, just feed the blob/buffer to the server.
Another path might be to "PUT" the file at the server.
Hope this helps.
I would ajax POST a base64 encoded string of the image and forget the whole file upload thingy. You can upload the canvas code directly and reconvert it server side if you need a preview or something or see what other outputs are available from your canvas/image converter code.
I am assuming you are uploading to same server so you would not have cross domain issue but otherwise you can setup your server to accept cross domain ajax request very easily.

Getting full file path of a clients file using a file dialog box

I am trying to allow users to upload pictures to the server.
I am trying to create a similar system to any website that has an 'attach' file or 'upload image' feature. All I need is to get the full path of the file select by the file dialog.
I tried using this for the file dialog with no success:
<input type="file">
This method does not provide the full file path, due to security reasons. My question is how can I create a similar input dialog to websites like tinypic, photobucket, etc.. that can help users input the full file path of a given image, into an input field?
I am aware that this cannot be done using the method above for security reasons, however, I have seen this done before on various websites without any problems, I was wondering what I had to do to implement a similar file dialog that helps fill in the text, which is a full file path, of an input field?
It is not possible to get the file full path on local machine using browser and javascript.
However, as you would like to upload the file to the server, the easy possibility I see is to use html form with input type file. You will receive the file on your http server when the form is submitted.
Here is a very good url http://www.cs.tut.fi/~jkorpela/forms/file.html that explains the whole process nicely.

loading an image through javascript

is there a way to load the full binary of an image in javascript?
what i want to do is to allow the user to preview an image before uploading it.
ie the user selects an image on his local drive (C:\image.jpg) , view it, and decides to upload or cancel.
i tried to set the source to the image path, but it didn't work since it is outside the webapplication project folder.
any help?
thx for your posts, but i ended up creating a temp folder on the server that stores the uploaded image using ajax. and when the user saves the data, the image is moved to another location, and the temp folder is deleted.
You can do something like this:
<img id="preview" src="" style="display:none;" />
<form>
....
<input type="file" id="file" />
....
</form>
<script type="text/javascript">
var file = document.getElementById("file");
var img = document.getElementById("preview");
file.onchange = function(){
img.src = file.value;
img.style.display = 'block';
};
</script>
There is no easy way, what You could do:
Preload image with some ajax file uploader to temp area and then let user decide
Use some third party written solution (f.ex. some flash component)
Here there is also similar question:
is it possible to preview local images before uploading them via a form?
You need server cooperation to access the image binary data. You won't be able to use the "Upload Form" to access the file info without uploading it to a server.
You could however do something like tell the user to paste the source binary data of the image in a textarea or something, then you can use JavaScript to load that binary data and display the actual image.
This is available in some browsers via the HTML5 file access API. Here is the Firefox documentation for file access.
As answered several times, you can't do this with plain HTML/JavaScript due to security restrictions. You need to send the image anyway. You can however handle this using a Java Applet since it runs entirely at the client machine and offers more control than HTML/JS. There are several free and ready-to-use ones. JumpLoader looks good and some major sites also uses it. With Flash it should also be possible, but I am not sure which ones offers this functionality. Check/tryout some Flash Multi File Uploaders.

Categories