Can any HTML or JavaScript API be used to get the file path in the input file [type = file]?
I think chrome supports this, but not sure if it's removed for security reasons. Anyway you can try playing with javascrip to set a custom width and height of some div and use the readAsDataURL of the uploaded file
<script type="text/javascript">
function uploadFile(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('#test').attr('src', e.target.result).width(100).height(100);
};
reader.readAsDataURL(input.files[0]);
}
}
</script>
<input type='file' onchange="uploadFile(this);" /> <img id="test" src="#" alt="my image" />
source to share
The jQuery File Upload Demo Page demonstrates the concept of getting the filename to upload. As files are added, the file name is displayed in a table below the controls.
The plugin has several options for enabling callbacks that are triggered after certain events.
Here's an example that shows how to get the filename:
function (e, data) {
$.each(data.files, function (index, file) {
alert('Added file: ' + file.name); // filename alerted
});
data.url = '/path/to/upload/handler.json';
var jqXHR = data.submit()
.success(function (result, textStatus, jqXHR) {/* ... */})
.error(function (jqXHR, textStatus, errorThrown) {/* ... */})
.complete(function (result, textStatus, jqXHR) {/* ... */});
}
According to the documentation, you have a better chance of success if you bind the callback function using the .bind
jQuery keyword :
$('#fileupload')
.bind('fileuploadadd', function(e, data) { ... } );
From the documentation on "add":
The data parameter allows you to override plugin parameters as well as define ajax parameters. data.files contains a list of files to request to upload: if the singleFileUploads option is enabled (which is the default), the add callback will be called once for each file in the XHR file upload selection, with the data.files array of length one, since each the file is downloaded separately. Another callback will be called once for each file selection.
Here is a complete list of jQuery File Upload Options .
Also, only a few browsers support image preview, which aims to access the filename:
Image preview
The following browsers support previewing images before uploading files:
- Firefox 3.6+
- Google chrome
- Opera 11 +
The Browser Support page contains detailed information on which plugin features are supported and not supported in different browsers.
source to share