简体   繁体   English

在laravel vuejs Axios中上传pdf文件

[英]Uploading pdf file in laravel vuejs Axios

I have created a project for bookshop and in the section of employee information I want to upload a PDF file using code given below.it give me the below error.I have been trying since few days but I could not get the solution if there is any sample way please guide me any help will be highly appreciated我为书店创建了一个项目,在员工信息部分,我想使用下面给出的代码上传 PDF 文件。它给了我以下错误。几天以来我一直在尝试,但如果有,我无法获得解决方案任何示例方式请指导我任何帮助将不胜感激

The given data was invalid.","errors":{"file":["The file field is required.给定的数据无效。","errors":{"file":["文件字段是必需的。

controller code is控制器代码是

public function store(Request $request)
  {
       $DocumentType= new DocumentType();
                  $this->validate($request,[
            'name'=>'required',
            'file' => 'required',                
        ]);

        $DocumentType->name = $request->input('name');
        $fileName = time().'.'.$request->file->extension();  
        $request->file->move(public_path('Files'), $fileName);
        $DocumentType->file =  $name;
         $DocumentType->save();
         return response()->json($DocumentType);

  }

Vue code is Vue代码是

<template>
  <div class="container">
 <div
      class="modal fade"
      id="addNew"
      tabindex="-1"
      role="dialog"
      aria-labelledby="addNewLabel"
      aria-hidden="true"
    >
      <div class="modal-dialog modal-dialog-centered" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <h5 class="modal-title" v-show="!editMode" id="addNewLabel">ثبت اسناد جدید</h5>
            <h5 class="modal-title" v-show="editMode" id="addNewLabel">تمدید اسناد</h5>

            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
              <span aria-hidden="true" style="margin-right: 317px;">&times;</span>
            </button>
          </div>
          <form
            @submit.prevent="editMode ? updateDocumentType() : createDocumentType()"
            enctype="multipart/form-data"
          >
            <div class="modal-body">
              <div class="form-group">
                <input
                  v-model="form.name"
                  placeholder="نام"
                  type="text"
                  name="name"
                  class="form-control"
                  :class="{ 'is-invalid': form.errors.has('name') }"
                />
                <has-error :form="form" field="name"></has-error>
              </div>
              <div class="form-group">
                <label for="file" class="col-sm-4 control-label">File</label>

                <div class="col-sm-12">
                  <input
                    type="file"
                    class="form-input"
                    :class="{ 'is-invalid': form.errors.has('file') }"
                    id="file"
                    name="file"
                  />
                  <has-error :form="form" field="file"></has-error>
                </div>
              </div>

              <div class="modal-footer">
                <button type="button" class="btn btn-danger" data-dismiss="modal">لغو</button>

                <button
                  v-show="editMode"
                  :disabled="form.busy"
                  type="submit"
                  class="btn btn-success"
                >تمدید</button>
                <button
                  v-show="!editMode"
                  :disabled="form.busy"
                  type="submit"
                  class="btn btn-primary"
                >ثبت</button>
              </div>
            </div>
          </form>
        </div>
      </div>
    </div>
</template>
  </div>
<script>
  createDocumentType() {
      // axios.get("api/getAllDocumentTypeDocumentType").then(response => {
      //   let data = response.data;

      if (this.form.name == "") {
        toast.fire({
          type: "warning",
          icon: "warning",
          html: "<h5>نام لازم است.</h5>"
        });
      }
      // else if (this.form.file == "") {
      //   toast.fire({
      //     type: "warning",
      //     icon: "warning",
      //     html: "<h5>لطفا،اسناد را انتخاب نماید.</h5>"
      //   });
      // }
      else {
        this.form
          .post("api/DocumentType")
          .then(() => {
            //    the below function will be use to reload the page

            //   this.$emit("refreshPage");
            $("#addNew").modal("hide");
            toast.fire({
              icon: "success",
              type: "success",
              html: "<h5> اسنادموافقانه اجاد گردید</h5>"
            });
            Fire.$emit("refreshPage");
            this.form.reset();
            //   this.$Progress.finish();
          })
          .catch(er => {
            console.log(er);
          });
      }
    }
</script>

Migration table code迁移表代码

 public function up()
    {
        Schema::create('document_types', function (Blueprint $table) {
            $table->bigIncrements('id');
             $table->string('name');            
            $table->string('file')->nullable();
             $table->softDeletes();
            $table->timestamps();
        });
    }

Code in API route API 路由中的代码

Route::apiResources(['DocumentType'=>'API\DocumentTypeController']);
Route::get('getAllDocumentType','API\DocumentTypeController@getAll');

The given data was invalid.","errors":{"file":["The file field is required.给定的数据无效。","errors":{"file":["文件字段是必需的。 This error is because there is no existing file in your payload upon submission of your form.此错误是因为提交表单时您的有效负载中没有现有文件。

in your form, bind data to your input file like what you did to your input name.在您的表单中,将数据绑定到您的输入文件,就像您对输入名称所做的一样。 like this像这样

<input
      type="file"
      v-model="form.file"
      class="form-input"
      :class="{ 'is-invalid': form.errors.has('file') }"
       id="file"
      name="file"
 />

Or, attach an event to this file input and process the file provided.或者,将事件附加到此文件输入并处理提供的文件。 like this像这样

<input
          type="file"
          class="form-input"
          :class="{ 'is-invalid': form.errors.has('file') }"
          id="file"
          name="file"
          @change="selectFile"
/>

Then in your methods, create also a selectFile function然后在你的方法中,创建一个 selectFile 函数

methods: {
    selectFile(file) {
         if(file == "") return false

         this.form.file = file.target.files
         console.log(this.form.file)
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM