我想你忘记了一点点复杂的问题: 如何获得真正的ImagePath?如果您想在不上载图像的情况下创建真实预览,则需要它。
我解决了这个问题并浪费了很多时间,因为从javascript获取路径几乎是不可能的。但最终找到了一个获得真正预览的方法。
我会在这里分享它,因为您的代码提供了足够的验证。所以我可以支持它完成。
当选择图像时,使用文件mannager或客户端的exporer创建临时路径,并且它是可访问的。因此,您必须使用方法:“URL.createObjectURL()”从该路径创建一个对象,然后将其附加到元素的“src”属性。 这里是一个代码片段作为样本:
1. a small styles sheet to easy encode
// myStyles.css
// ...
.collapsed-element { diplay: none; }
// ...
2. A hidden container element to prepare the previewed image
<!-- my html page !-->
<!-- the workhorse in a hidden container -->
<div id="canvas-container" class="collapsed-element">
<img src="" class="canvas" id="canvas-input">
</div><div class="clear-block"></div>
3. The widget to have a working input set
<!-- The imput set: a table-row with a file input and a related img-preview element -->
<table id="my-image-getter-widget">
<tr>
<td style="width: 15%" id="prw-container">
<!-- fibopad.gif is a transparent pad using Fibonacci numbers as dimentions (a personal preference) -->
<!-- graphema will be the file input name; so I asign it as a class-prefix attribute in the related viewer -->
<img src="/path_to_a_transparent_image_pad/fibopad.gif" alt="" title="" width="46" height="75" class="graphema-vwr"/>
</td>
<td id="the-input-set-column" style="width: 85%">
<!-- a text input to show the old path if you are editing and have an older loaded image -->
<!-- note the "graphema" word used elsewhere to relate elements and get a way to easy encode -->
<div class="container-inline">
<div class="form-item" id="edit-graphema-txt-wrapper">
<label for="edit-graphema-txt">Image File:</label>
<input type="text" maxlength="255" name="graphema_txt" id="edit-graphema-txt" size="45" value="" class="form-text"/>
</div>
</div>
<!-- The image input note the name="files[graphema]" attribute as array item -->
<!-- the class "enabled-prvw" will be added to inputs at onload event and will be used by javascript code to bind behaviors and get the preview -->
<div class="container-inline">
<div class="form-item" id="edit-graphema-wrapper">
<label for="edit-graphema">Load a image:</label>
<input type="file" name="files[graphema]" class="form-file" id="edit-graphema" size="60"/>
</div>
</div>
<!-- A helper tip for users -->
<div class="description">
<br/>Blah, blah ... blah ...
</div>
</td>
</tr>
</table><!-- widget end -->
4. Finaly! the javascript
// Filename: myimage-preview.js
// a function to properly hall the preview image with a good container's resizing
function set_dimentions_for_previewer(curwidth, curheight, standarlimit, targetid){
var nw = standarlimit;
if(curheight > 10){ nw = parseInt((curwidth * standarlimit)/curheight); }
$(targetid).attr('height', standarlimit); $(targetid).attr('width', nw);
}
// a function to bind events and show selections (do not performs validation, so you must add it)
// I'd commented my debug lines cause my modal dialog is not present
function enable_preview_for_imageinput(){
// var ondebug = Settings.lgq_debug_js_enabled;
$("input:file").each(function(){
var id = '' + $(this).attr('id');
var idselector = '#'+id;
// if(ondebug === 1){ estatus_add('binded change event<br>'); }
if(!$(idselector).hasClass('enabled-prvw')){
$(idselector).bind("change", function(event){
var chunk = id.split('-', 2);
var imgclass = '.'+chunk[1]+'-vwr';
// /*
$('img.canvas').bind('load', function(){
// if(ondebug === 1){ status_add('Image added to canvas <br>'); }
var curheight = $('img.canvas').attr('height'), curwidth = $('img.canvas').attr('width');
// if(ondebug === 1){ status_add('dimentions width ='+curwidth+', height ='+curheight+' <br>'); }
set_dimentions_for_previewer(curwidth, curheight, 75, imgclass);
$('img.canvas').unbind('load');
});
$('.canvas').attr('src',URL.createObjectURL(event.target.files[0]));
$(imgclass).fadeIn("fast").attr('src',URL.createObjectURL(event.target.files[0]));
// var tit = 'Change event at '+id;
// var msg = ''+$(idselector).val();
// message_sys(tit, msg);
});
$(idselector).addClass('enabled-prvw')
}
// if(ondebug === 1){ estatus_add('Binded input: '+id+' \n'); }
});
// if(ondebug === 1){ message_sys('Load success', get_status()); }
}
// an onload call to enable all
$(document).ready(function(){
enable_preview_for_imageinput();
});
非常感谢:)我认为.match()可能会更好,如果它匹配(/ http:\/\/\ S +(\。png | \ .jpg | \ .gif)/ g); '?此外,试图找出它 - 但图像预览似乎出现 - 控制台显示错误“资源解释为图像,但与MIME类型应用程序/ json.' [在Chrome] – Tom
传输即,因为这对我来说[http: /educ661-373wikiarticle.wikispaces.com/file/view/test.jpg/149584695/test.jpg]?另外,如果我删除输入链接 - 预览仍然存在? – Tom
我来看看。我正在使用Firefox,没想到要尝试其他任何事情。 – Henry