简体   繁体   English

文件上传是否可以通过ajax POST发送?

[英]file upload be accessed to send through ajax POST?

here's the situation 这是情况

Within a single form 1) User clicks on the 'browse' button, which opens a dialog to choose an image file to upload. 在一个单一的形式1)用户点击“浏览”按钮,打开一个对话框,选择一个图像文件进行上传。 ie

input id='img_upload' name="ufile" type="file" value="Browse"/>

2) With file selected, user then clicks a button 'Upload file' button, which calls a js function ie 2)选择文件后,用户单击“上传文件”按钮,该按钮调用js函数,即

input type='button' id='uload' class='save2' value='Upload file' onclick='upload_img()' >   </input>

What I would like upload_img() to do is send the contents of the browse via POST through something like AJAX to a .php file called image_upload.php, which will save the img and output an tag displaying the img file. 我想upload_img什么()做的是通过类似AJAX来叫image_upload.php一个PHP文件,这将节省的IMG和输出显示在img文件的标签通过发送POST浏览的内容。 I have an iframe which displays the contents of image_upload.php 我有一个iframe,可显示image_upload.php的内容

If possible, how can I access the contents of a 'browse' and send it through POST 如果可能的话,我怎么可以访问“浏览”的内容,并通过POST发送

Cheers! 干杯!

EDIT Sorry I forgot that a strict subset of html is supported, so I had to remove the left most < 编辑抱歉,我忘记了支持HTML的严格子集,因此我不得不删除最左边的<

If you don't mind the user needing flash and that you have to use jquery, uploadify does pretty much exactly what you just requested. 如果您不介意需要使用Flash的用户并且必须使用jquery,则uploadify几乎可以完全满足您的要求。 You control the callback so you can do whatever you need after the file uploads. 您可以控制回调,以便在文件上传后可以执行所需的任何操作。

http://www.uploadify.com/ http://www.uploadify.com/

I'm guessing you're hoping to pull the file path from the file selection action, send it to the php script, which will use the file path to upload the actual image. 我猜想您希望从文件选择操作中提取文件路径,并将其发送到php脚本,该脚本将使用文件路径上传实际图像。

In short: this is not possible . 简而言之:这是不可能的 The PHP script is executed on the server, and therefore has no access to the client's filesystem. PHP脚本是在服务器上执行,因此具有到客户端的文件系统的访问权限。 However, this does not mean that AJAX file uploads are impossible altogether. 但是,这并不意味着AJAX文件上传是不可能完全。 There are several jQuery-utilizing solutions that should do the trick. 有几种使用jQuery的解决方案可以解决这个问题。 This article features seven of them. 本文介绍了其中的七个。

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

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