我想用jQuery异步上传一个文件.这是我的HTML:
<span>File</span>
<input type="file" id="file" name="file" size="10"/>
<input id="uploadbutton" type="button" value="Upload"/>
Run Code Online (Sandbox Code Playgroud)
在这里我的__CODE__代码:
$(document).ready(function () {
$("#uploadbutton").click(function () {
var filename = $("#file").val();
$.ajax({
type: "POST",
url: "addFile.do",
enctype: 'multipart/form-data',
data: {
file: filename
},
success: function () {
alert("Data Uploaded: ");
}
});
});
});
Run Code Online (Sandbox Code Playgroud)
我只获取文件名,而不是上传文件.我该怎么做才能解决这个问题?
我正在使用jQuery Form Plugin上传文件.
我有一个表单,通过ajax向服务器提交值.
<form>
<input id="titlee" name="titlee">
<input type="file" name="fileToUpload" id="fileToUpload">
<button type="submit" value="submit" id="submit" name="submit">Start</button>
<div class="progress"></div>
</form>
Run Code Online (Sandbox Code Playgroud)
脚本
<script type="text/javascript">
$(function()
{
$("#submit").click(function()
{
var titlee = $("#titlee").val();
var fileToUpload= $("#fileToUpload").val();
var dataString = 'titlee='+ titlee + '&fileToUpload=' + fileToUpload;
$.ajax(
{
type: "POST",
url: "c_insert_test.php",
data: dataString,
success: function()
});
return false;
});
});
</script>
Run Code Online (Sandbox Code Playgroud)
c_insert_test.php
<?php
$titlee = $_POST['titlee'];
$target_dir = "reqdoc/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
$new_filename = $target_dir . uniqid() …Run Code Online (Sandbox Code Playgroud) 我是 jQuery 新手,现在我正在处理文件上传。我想在每次上传图片时添加一些进度条。我uploadProgress在 jQuery 中使用了,但它似乎不起作用。这是我的代码:
$('#_form_').on('submit', function(e){
var file_and_desc = new FormData($(this)[0]),
form_url = "_pages/_form_";
var ext = choose.val(),
allowed = ['jpg','png'];
if(ext){
var get_ext = ext.split('.');
get_ext.reverse();
if($.inArray(get_ext[0].toLowerCase(), allowed) > -1){
//upload image
$.ajax({
url : form_url,
type: 'POST',
data: file_and_desc,
contentType: false,
processData: false,
uploadProgress: function(event, positio, total, percentComplete){
$('h1').html(percentComplete);
},
success: function(data){
// some code here...
}
});
}
}
});
Run Code Online (Sandbox Code Playgroud)
就是这样!我该怎么办?
我有一个脚本,需要几秒钟的处理,最多约一分钟.该脚本调整图像数组的大小,锐化它们并最终将它们拉链以供用户下载.
现在我需要某种进度信息.我在想,使用jQuery的.post()方法,来自回调函数的数据会逐步更新,但这似乎不起作用.
在我的例子中,我只是使用循环来模拟我的脚本:
$(document).ready(function() {
$('a.loop').click(function() {
$.post('loop.php', {foo:"bar"},
function(data) {
$("div").html(data);
});
return false;
});
});
Run Code Online (Sandbox Code Playgroud)
loop.php:
for ($i = 0; $i <= 100; $i++) {
echo $i . "<br />";
}
echo "done";
Run Code Online (Sandbox Code Playgroud) javascript ×4
jquery ×4
ajax ×2
asynchronous ×1
callback ×1
file-upload ×1
http-post ×1
mysql ×1
php ×1
progress-bar ×1