无法获取具有'Content-Type':'multipart / form-data'的.post来工作Axios React Native

时间:2019-09-13 06:51:26

标签: react-native post file-upload axios content-type

我正在尝试上传 FormData ,其中包括文本,图像,pdf等

我正在使用 axios

  

Axios通话

const sellerRegister = params => {
  console.log(params);
  return dispatch => {
    dispatch(servicePending());
    return axios
      .post(`${BaseUrl}/seller/register`, params, {
        headers: {
          Accept: "application/json",
          "Content-type": `multipart/form-data; boundary=${params._boundary}`
        }
      })
      .then(res => {
        return dispatch(sellerRegisterSuccess(res));
      })
      .catch(err => {
        return dispatch(serviceError(err.message));
      });
  };
};
  

FormData中的参数

include **text,pdf,image**

  

ServiceError

enter image description here

提供此错误的解决方案。并指导我是否以正确的方式执行此操作?

谢谢

1 个答案:

答案 0 :(得分:1)

使用最新版本的Axios,您可以在标头中使用“ Content-Type”:“ multipart / form-data”发出HTTP请求,如下所示:

<?php
$servername = "localhost";
$username = "root";
$password = "<?>";
$dbname = "<?>";
?>

<?php

error_reporting(E_ALL);

ini_set("display_errors", 1);

?>

<?php

require_once('connect.php');

require_once('config.php');

require('PHPMailer/PHPMailerAutoload.php');

if(isset($_POST) & !empty($_POST)){

$name = $_POST['name'];

$email = $_POST['email'];

$subject = $_POST['subject'];

$message = $_POST['message'];

if(!isset($name) || empty($name)){

$error[] = "Name is required";
}

if(!isset($email) || empty($email)){

$error[] = "E-Mail is required";
}

if(!isset($subject) || empty($subject)){

$error[] = "Subject is required";
}

if(!isset($message) || empty($message)){

$error[] = "Message is required";
}

if(!isset($error) || empty($error)){

$to = "vivek@codingcyber.com";

$headers = "From : " . $email;

$mail = new PHPMailer();

$mail->isSMTP();

$mail->SMTPSecure = 'tls';

$mail->Host = $smtphost;

$mail->SMTPAuth = true;

$mail->Username = $smtpuser;

$mail->Password = $smtppass;

$mail->SMTPSecure = 'ssl';

$mail->Port = 465;

$mail->CharSet = 'UTF-8';

$mail->setFrom('', 'PixelW3 Technologies');

$mail->addAddress('', ''); 

$mail->Subject = $subject;

$mail->Body    = $message . " Name : " . $name . ". E-Mail : " . $email;

if(!$mail->send()) {

    echo 'Message could not be sent.';

    echo 'Mailer Error: ' . $mail->ErrorInfo;

} else {

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $stmt = $conn->prepare("INSERT INTO `contact` (name, email, subject, message) VALUES (:name, :email, :subject, :message)");
    $stmt->bindParam(':name', $name);
    $stmt->bindParam(':email', $email);
    $stmt->bindParam(':subject', $subject);
    $stmt->bindParam(':message', $message);
    $stmt->execute();
}catch(PDOException $e)
{
echo "Error: " . $e->getMessage();
}
$conn = null;

/*
if(mail($to, $subject, $message, $headers)){

    $sql = "INSERT INTO `contact` (name, email, subject, message) VALUES ('$name', '$email', '$subject', '$message')";

    if(mysqli_query($connection, $sql)){

        "Message Received, we will get back to you soon";

    }else{
        echo "Failed to send message, Try again";

    }

}else{

    echo "Failed to send message, Try again";

}

*/
}


}

?>
<!DOCTYPE html>

<html>

<head>

<title>Simple Contact form in PHP & MySQL</title>

<!-- Latest compiled and minified CSS -->

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" >

</head>

<body>

<div class="container">

<div class="row">

    <form class="col-md-6 col-md-offset-3" method="post">

        <h2>Contact Us</h2>

      <div class="form-group">

        <label for="exampleInputEmail1">Name</label>

        <input type="text" name="name" class="form-control" id="exampleInputEmail1" placeholder="Enter Your Full Name" required="">

      </div>

      <div class="form-group">

        <label for="exampleInputEmail1">Email address</label>

        <input type="email" name="email" class="form-control" id="exampleInputEmail1" placeholder="Enter Your Email" required="">

      </div>

      <div class="form-group">

        <label for="exampleInputPassword1">Subject</label>

        <input type="text" name="subject" class="form-control" id="exampleInputPassword1" placeholder="Enter Your Subject" required="">

      </div>

      <textare`enter code here`a class="form-control"  name="message" rows="3" placeholder="Enter Your Query Here" required=""></textarea>

      <button type="submit" class="btn btn`enter code here`-default">Submit</button>

    </form>

</div>
</div>

</body>

</html>

使用url而不是uri。

或 您可以简单地做:

const formData = new FormData();
formData.append('action', 'ADD');
formData.append('param', 0);
formData.append('secondParam', 0);
formData.append('file', new Blob(['test payload'], { type: 'text/csv' }));
axios({
  url: 'http://your_host/api/auth_user',
  method: 'POST',
  data: formData,
  headers: {
    Accept: 'application/json',
    'Content-Type': 'multipart/form-data'
  }
})