使用Angular JS和PHP上传文件
我正在尝试使用formdata对象上传一个带有Angular JS和PHP的文件使用Angular JS和PHP上传文件,php,angularjs,Php,Angularjs,我正在尝试使用formdata对象上传一个带有Angular JS和PHP的文件 fd.append("file", element[0].files[0]) ??? does this actually append the file data 我想使用key1(data)值作为JSON,这样我就可以将文件名和用户信息等传递给php脚本 fd.append("file", element[0].files[0]) ??? does this actually appe
fd.append("file", element[0].files[0]) ??? does this actually append the file data
我想使用key1(data)值作为JSON,这样我就可以将文件名和用户信息等传递给php脚本
fd.append("file", element[0].files[0]) ??? does this actually append the file data
我想使用key2(uploadFile),值为正在上载的文件
fd.append("file", element[0].files[0]) ??? does this actually append the file data
我可以使用POSTMAN设置一个键(命名数据)并为一些JSON数据{“username”:“kk1”}添加一个值
然后设置一个键(名为uploadFile)和一个指向测试文件的值。上传_test1.txt
PHP成功,上传文件upload_test1.txt并保存到正确的位置
fd.append("file", element[0].files[0]) ??? does this actually append the file data
当我使用Angularjs运行测试时
PHP响应并表示索引上传文件不存在
fd.append("file", element[0].files[0]) ??? does this actually append the file data
我唯一能想到的是文件路径设置不正确
fd.append("file", element[0].files[0]) ??? does this actually append the file data
请参阅下面的代码片段
fd.append("file", element[0].files[0]) ??? does this actually append the file data
任何帮助都将不胜感激
fd.append("file", element[0].files[0]) ??? does this actually append the file data
KNK53
fd.append("file", element[0].files[0]) ??? does this actually append the file data
Angular username=“kk1”id=“1”和filename=“C:\temp\upload\u test2.txt”
...
bdy={
“用户名”:用户名,“id”:id}
fd.append("file", element[0].files[0]) ??? does this actually append the file data
fd.append("file", element[0].files[0]) ??? does this actually append the file data
//PHP回显json,然后回显“from variable:kk1”第1-3行
//然后错误通知:未定义索引:uploadFile第5行
PHP
fd.append("file", element[0].files[0]) ??? does this actually append the file data
1$data=json\u decode($\u POST[“data”],true)//应该有一个用户名
2回显“获取文件:”$_POST[“数据”];
3 echo“来自变量:”$数据[“用户名”];
4回声“
”;
5$user=$\u文件['uploadFile']['tmp\u name'];
回显“文件名:”$用户
';
回显“dir:”。ini_get('upload_tmp_dir');
$target_dir=“../uploads/”;
$target\u file=$target\u dir'gotfile.txt';
如果(移动上传的文件($上传文件[“上传文件”][“tmp\U名称”],$target\U文件)){
回声"移动成功",;
}
其他的
echo“移动失败”;
我也得出了这个结论。我是网络编程新手,但有很多其他编程经验。FormData对象append方法实际上并不上载文件
fd.append("uploadFile", filename);
fd.append("file", element[0].files[0]) ??? does this actually append the file data
最好使用指令,然后合并FileReader对象来读取文件数据吗
fd.append("file", element[0].files[0]) ??? does this actually append the file data
或
fd.append("file", element[0].files[0]) ??? does this actually append the file data
我在其他堆栈溢出线程上看到
fd.append("file", element[0].files[0]) ??? does this actually append the file data
某些答案仅使用指令访问输入元素的“FILES”属性
fd.append("file", element[0].files[0]) ??? does this actually append the file data
参见此线的标记57
fd.append("file", element[0].files[0]) ??? does this actually append the file data
KNK53所以我重新定义了函数
fd.append("file", element[0].files[0]) ??? does this actually append the file data
使用:
fd.append(“文件”,元素[0]。文件[0]
fd.append("file", element[0].files[0]) ??? does this actually append the file data
这会将信息作为$\文件发送到PHP
fd.append("file", element[0].files[0]) ??? does this actually append the file data
使用文件读取器和onreadend事件:
fd.append("file", element[0].files[0]) ??? does this actually append the file data
onreadend = function(tgt) {
fdata = tgt.target.result;
fd.append("file", fdata);
.
.
.
}
This approach sends the data to $_POST in PHP
KNK53HTML:
fd.append("file", element[0].files[0]) ??? does this actually append the file data
<input type="text" ng-model="userName" />
<input type="file" your-directive ng-model="userFile" />
在控制器中,假设文件发布在链接到控制器的表单提交事件上:
fd.append("file", element[0].files[0]) ??? does this actually append the file data
$scope.submit = function() {
var formData = new FormData();
...
formData.append('userName', $scope.userName);
formData.append('userFile', $scope.userFile, $scope.userFile.name);
...
$http({
method: 'POST',
url: <apiUrl>,
data: formData,
headers: {'Content-Type': undefined} // <-- don't forget this
...
}).then...
};
抱歉,编辑太多…分心了您在这里只是以文本形式发送文件名,没有执行实际的文件上载。“我唯一能想到的是文件路径设置不正确。”-听起来你以为你可以从客户端上传任意文件,只需在代码中自己指定路径即可…?当然不能,用户需要主动选择文件。
fd.append("file", element[0].files[0]) ??? does this actually append the file data