简体   繁体   English

如何使用AJAX上传和预览上传的图像

[英]How to upload and preview the uploaded image using AJAX

I have created the following web page: 我创建了以下网页:

...
<img alt="" src="images/frame.png" id="contact-image" />
<form enctype="multipart/form-data" action="PHPScripts/upload.php" method="POST">
    <input type="file" id="browseContactImageButton" name="image"
           accept="image/jpg,image/jpeg,image/png,image/gif"
           onchange="this.form.submit();" />
</form>
...

I want to be able to upload the the image to file system or database (haven't decided yet) and to be able to see the uploaded image in the <img id="contact-image" /> immediately after the file has been uploaded, without reloading the page, ie using AJAX. 我希望能够将图像上传到文件系统或数据库(尚未决定),并且能够在文件被保存后立即在<img id="contact-image" />查看上传的图像。上传,而无需重新加载页面,即使用AJAX。

Can you show me the concept that I should follow? 您能告诉我我应该遵循的概念吗? I know that I am making a mistake by calling the PHPScripts/upload.php on form submitting. 我知道我在提交表单时调用PHPScripts/upload.php犯了一个错误。 My guess is that I should call some javascript function instead, but where do I go from there and what should that function look like? 我的猜测是我应该改为调用一些javascript函数,但是我从那里去哪里,该函数应该是什么样? Thanks. 谢谢。

With MVC or any other backend it's fairly simple. 使用MVC或任何其他后端,这非常简单。 Let's assume you have a controller with the name UploadPicture. 假设您有一个名为UploadPicture的控制器。 When the webserver has finished uploading the picture, you would simply make the server return json with the image path or whatever you need. 当网络服务器上传完图片后,您只需使服务器返回带有图像路径或所需内容的json。

I would recommend using jQuery since it's an excellent framework to work with Javascript and has a good cross-browser support. 我建议使用jQuery,因为它是使用Javascript的出色框架,并且具有良好的跨浏览器支持。

I've done this before and it was just like what I described above. 我之前已经做过,就像我上面描述的那样。

IF you happen to know the path and image name, then you wouldn't even need to return anything from the server. 如果您碰巧知道路径和映像名称,那么您甚至不需要从服务器返回任何内容。 You would simply do with jQuery when the upload has finished: 上传完成后,您只需使用jQuery:

$("#contact-image img").attr('src', 'http://PathToImage');

And you could use $.ajax to trigger when the submit has finished. 您可以使用$ .ajax在提交完成后触发。

Reference on $.ajax - http://api.jquery.com/jQuery.post/ $ .ajax上的参考-http: //api.jquery.com/jQuery.post/

I hope this helps. 我希望这有帮助。

Try the Ajax upload 尝试Ajax上传

You can find explanations and examples it here: 您可以在此处找到说明和示例:

http://www.zurb.com/playground/ajax_upload http://www.zurb.com/playground/ajax_upload

have you tried swfupload ? 你尝试过swfupload吗?

here is a demo that uses php 这是一个使用PHP的演示

I will suggest you to use the YUI file upload or JQuery plug in for file upload. 我建议您使用YUI文件上传JQuery插件进行文件上传。 Once the file is upload you can retrieve the file path from the server and update using the JS on the client side. 上传文件后,您可以从服务器检索文件路径,并使用客户端上的JS更新。

var imgControl = document.getElementById('image');

imgControl.src = filePathFromServer;

And if you want to try something new HTML5 ... i will suggest to have a look at this demo . 如果您想尝试一些新的HTML5 ,我建议您看一下这个演示

Here's the best solution in my opinion. 我认为这是最好的解决方案。 It doesn't involve jQuery and it provides AJAX-like functionality. 它不涉及jQuery,它提供了类似AJAX的功能。 Thank you to everyone who has put an effort in helping me resolve this. 感谢所有努力帮助我解决此问题的人。 Cheers! 干杯!

http://www.articlealley.com/article_573896_81.html http://www.articlealley.com/article_573896_81.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM