gpt4 book ai didi

php - 使用jquery上传文件

转载 作者:可可西里 更新时间:2023-11-01 01:07:58 24 4
gpt4 key购买 nike

我想用ajax上传一个文件

这是我的代码PHP, HTML:

<form action="uploadVideo.php" method="POST" enctype="multipart/form-data">  
<input type="file" name="choosefilebtn" id="choosefilebtn" size="50" />
<input type="button" class="uploadbutton" value="upload" name="uploadbtn" id="uploadbtn" />
</form>

j查询:

$(function() {
$('#uploadbtn').click(function() {
var filename = $('#choosefilebtn').val();
alert(filename);
$.ajax({
type: "POST",
url: "uploadVideo.php",
enctype: 'multipart/form-data',
data: {
file: filename
},
success: function() {
alert("Data Uploaded: ");
}
});
});
});

当我使用 type sumbmit 作为上传按钮时(没有 ajax)它可以工作,但是使用 ajax 它不起作用,任何人都可以帮助我,谢谢


编辑:添加了 uploadVideo.php 代码

$publish->remotehost=$ftpremotehost;
$publish->username=$ftpusername;
$publish->password=$ftppassword;
$publish->remotedir=CONSTANT_SERVERROOT.$folderName;
$publish->filename=$_FILES['choosefilebtn']['name'];
$publish->FTPLogin();
$publish->filecontent = fread( fopen($_FILES['choosefilebtn']['tmp_name'], "rb"),
$_FILES['choosefilebtn']['size']);
$publish->makedir($publish->remotedir);
$result=$publish->Publish();

最佳答案

您会注意到在 ajax 调用中您发送的是文件名,而不是该文件的内容:

    $.ajax({
//...
data: {
file: filename //just a name, no file contents!
},
//...
});

据我所知,通过 ajax 发送文件数据的唯一方法是使用隐藏的 iframe 并向其提交表单

即有

<iframe style="display: none" id="formtarget" />
<form action="uploadVideo.php" method="POST" enctype="multipart/form-data"
target="formtarget">
<input type="file" name="choosefilebtn" id="choosefilebtn" size="50" />
<input type="button" class="uploadbutton" value="upload" name="uploadbtn" id="uploadbtn" />
</form>

关于php - 使用jquery上传文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6831883/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com