我正在使用XMLHttpRequest将文件上传到php脚本。问题是我无法从客户端更改文件名,我正在努力找出如何使用文件发送变量。
我在屏幕上有一个文本框,基本上当用户提交文件时,我希望文本框中的文本转到php文件中。
这是我的上传功能:
function uploadFile() {
var fd = new FormData();
var count = document.getElementById('fileToUpload').files.length;
for (var index = 0; index < count; index ++)
{
var file = document.getElementById('fileToUpload').files[index];
fd.append('myFile', file);
}
var xhr = new XMLHttpRequest();
xhr.upload.addEventListener("progress", uploadProgress, false);
xhr.addEventListener("load", uploadComplete, false);
xhr.addEventListener("error", uploadFailed, false);
xhr.addEventListener("abort", uploadCanceled, false);
xhr.open("POST", "savetofile.php");
xhr.send(fd);
}
并在php文件中:
<?php
if (isset($_FILES['myFile'])) {
// $text = contents of the textbox that has been sent from javascript
move_uploaded_file($_FILES['myFile']['tmp_name'], "daa_coke/images/" . $_FILES['myFile']['name']);
$xml = simplexml_load_file('daa_coke/newcoke.xml');
$employee = $xml->addChild('flight');
$employee->addChild('to', 'dsd');
$employee->addChild('from', 'Gary');
$employee->addChild('imagepath', $_FILES['myFile']['name']);
$employee->addChild('templateStyle', 'template1');
$employee->addChild('time', '13:37');
$employee->addChild('date', '24/12/16');
file_put_contents('daa_coke/newcoke.xml', $xml->asXML());
}
?>
我只是希望能够传递文本框的内容,以便将其输出到$ test变量中。
答案 0 :(得分:0)
filename
应该是传递给.append()
formData.append(name, value, filename);
e.g;
fd.append("myFile", file, this.querySelector("input[type=text]").value);
答案 1 :(得分:0)
fd.append('textBoxName',textBoxValue)工作