【发布时间】:2014-03-21 10:09:59
【问题描述】:
这是我的sn-p ajax 一个上传提交文件的服务器端脚本。它在input[type=file] 的change 上触发。
$('#magLogo').on('change', function(e) {
var $input = $(this);
var file = e.currentTarget.files[0];
var data = new FormData();
data.append('file', file);
$.ajax({
type: "POST",
data: data,
cache: false,
contentType: false,
processData: false,
url: 'my/path/to/upload/script.php'
})
.done(function(fullPath) {
$input
.parents('#magLogoCont')
.css('background-image', "url('"+fullPath+"')");
});
});
另一方面,PHP 脚本返回一个 JSON 字符串,其中包含上传文件的临时目录(我在 Windows 上运行时看起来像 file:///C:/wamp/tmp/filename.jpg)。
问题是当我想将此图像作为 div 的背景以向用户显示它已上传时,我在控制台中收到的错误为 Not allowed to load local resource)。我无法将它直接上传到最终目录中,因为在用户选择要上传的图像时,所有信息都不可用。
有没有办法做我想做的事?
非常感谢!
【问题讨论】:
标签: php jquery ajax upload tmp