我的 .html
文件中有此代码:
<form action="upload.php" method="post" enctype="multipart/form-data">Select file to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload file" name="submit">
</form>
我应该修改代码的哪些部分?
最佳答案
您需要使用ajax将文件上传到服务器:
HTML 代码:
<form action="#" method="post" enctype="multipart/form-data">Select file to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload file" name="submitForm" name="submitForm">
</form>
jQuery 代码可能是这样的:
$('#submitForm').on('click', function() {
var file_data = $('#fileToUpload').prop('files')[0];
var form_data = new FormData();
form_data.append('file', file_data);
$.ajax({
url: 'upload.php', // point to server-side PHP script
dataType: 'text', // what to expect back from the PHP script, if anything
cache: false,
contentType: false,
processData: false,
data: form_data,
type: 'post',
success: function(php_script_response){
alert(php_script_response); // display response from the PHP script, if any
}
});
});
我很想知道这段代码是否适合您
关于php - 如何在按下打开 php 脚本的上传按钮后保留在 .html 上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36200426/