【问题标题】:How to make the attachement downloadable using PHP and VueJS如何使用 PHP 和 Vue JS 使附件可下载
【发布时间】:2020-10-28 11:48:32
【问题描述】:

这是我的 view.vue 文件,其中包含一个让用户填写的表单。

<v-form ref="form" lazy-validation style="width:70%;margin:4% auto;" ENCTYPE="text/plain">
       <input type="text" v-model="ob_name">
       <input type="file" ref="file1">
       <v-btn @click="createContact()">Confirm</v-btn>
</v-form>

然后这是我在 view.vue 下的脚本,我试图控制该文件并且它工作正常,它将在 DevTools 控制台中显示这个 (file1: ► File)。

createContact: function(){
    formData.append('file1', this.file1);
    formData.append('ob_name', this.ob_name);
    this.axios({ 
          method: 'post',
          url: 'http://www.company.com/mail.php',
          data: formData,
          config: { 
          headers: {
                'Content-Type': 
                'multipart/form-data' 
          }}
    })
},

在 mail.php 中。我也尝试过输入文本而不是文件类型,它也可以。我可以看到电子邮件中的内容。唯一无法查看的是 Type="File"。

<?php

$file1= "Undefined SSM";
$ob_name = "Undefined name";
if(isset($_POST['file1'])){
    $file1= $_POST['file1'];
    $ob_name = $_POST['ob_name'];
}

$message = "<p>Download the file below</p>";
$message .= "<p>$file1 $ob_name</p>";

$to_email = 'myemail@gmail.com';
$subject = 'New registration';
$headers[] = 'MIME-Version: 1.0';
$headers[] = 'Content-type: text/html; charset=UTF-8';
$headers[] = 'From: Company <noreply@Company.com>';

mail($to_email, $subject, $message, implode("\r\n", $headers));

?>

我真的需要帮助,我为这个问题苦苦挣扎了很长时间。不知道该怎么办。

这是我从 php 收到的电子邮件内容,它提到了未定义的名称

当我单击按钮并将文件触发到 mail.php 时,这是否正确 formData.append('ob_ssm', this.file,this.file.name);

"

【问题讨论】:

  • 您遇到的错误是什么
  • 我已经上传了关于电子邮件中显示的数据信息的图片
  • $file1= $_POST['file1']; - 文件上传在 $_POST 开始时不可用。去正确地了解自己正在使用的技术/功能的绝对基础真的很难吗? php.net/manual/en/features.file-upload.post-method.php

标签: javascript php vue.js email


【解决方案1】:

要使附件可下载,请使用此标头。

header("Content-Disposition: attachment; filename=${filename}");

【讨论】:

  • 我不知道为什么,每当我添加 $file1="Undefined SSM";关于文件的任何信息,我都会收到这封电子邮件 公司名称未定义名称未定义 SSM
  • 我认为要访问文件,您需要使用 $_FILES 全局对象而不是 $_POST,抱歉,我没有收到您的最后评论。
  • formData.append('ob_ssm', this.file,this.file.name);
  • 顺便说一句,非常感谢您对我的帮助,在我更改了 $_FILES 后,一切正常,但附件仍然没有
  • 您想在邮件中提供附件??
猜你喜欢
  • 2014-03-19
  • 2022-01-26
  • 2011-09-06
  • 2021-02-02
  • 2020-07-29
  • 2023-03-24
  • 2019-05-15
  • 1970-01-01
  • 2019-05-19
相关资源
最近更新 更多