我正在尝试构建一个小型应用,它将multipart/form-data
发送包含两个数据项的表单:
图片中附有应用程序块到此邮件。 我不知道如何使用文件和基于文本的表单数据发送multipart/form-data
。需要您对此提出建议。你能帮忙吗?
对于那些不熟悉Content-Type = multipart / form数据的人来说,这里有一个关于它的内容:
内容类型" application / x-www-form-urlencoded"效率低下 用于发送大量二进制数据或包含的文本 非ASCII字符。内容类型" multipart / form-data"应该 用于提交包含文件,非ASCII数据和表单的表单 二进制数据。
更多详情可在以下网址找到: https://www.w3.org/TR/html401/interact/forms.html#successful-controls
我已经找到了使用get和put方法实现它的方法。我希望使用Post
方法实现此目的。
<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$user = $_POST["username"];
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
$uploadOk = 0;
}
if ($uploadOk == 0) {
echo "Upload failed with reason = $uploadFailureReason <br>";
}
else {
echo "Upload Sucessful <br>";
$pyscript = '/home/ubuntu/workdir/src/httppython.py';
$python = '/home/ubuntu/.virtualenvs/cv/bin/python';
$filePath = $target_file;
$cmd = "$python $pyscript --image $filePath --user $user";
//echo "command = $cmd ";
exec("$cmd", $output);
echo "<input type=button onClick=\"location.href='upload_new.html'\" value='New Upload'>"
}
?>