Загрузка файла Vue с параметрами - PullRequest
0 голосов
/ 28 февраля 2019

Привет, ребята. Я создаю компонент для загрузки файлов и его работы до сих пор, но наряду с данными я хотел бы также передать некоторые параметры, например

HTML

<div class="col-md-4">
      <div class="container">
        <div class="large-12 medium-12 small-12 cell">
          <label>
            Files
            <input type="file" name="file" ref="files" multiple v-on:change="fileChange($event.target.files)" />
          </label>
          <v-btn outline color="primary" dark v-on:click="upload()">Submit</v-btn>
        </div>
      </div>
  </div>

Сценарий

import api from '../store/api.js'
  import axios from 'axios'

  export default {
    name: 'Profile',
    data() {
      return {
        records: [],
        application: [],
        profile: [],
        history: [],
        userValues: [],        
        files: new FormData()
     },
    fileChange() {
        for (var key in event.target.files) {
          this.files.append('files', event.target.files[key]);
        }
      },
      upload() {
          axios.post('/api/upload', this.files)
      .then(result => {
      console.dir(result.data);
    }, error => {
      console.error(error);
    });
  }

здесь (axios.post ('/ api / upload', this.files)) я хотел бы включить email: this.profile.email

Поскольку я добавляю этот параметр к имени файла на моем бэкэнде

Контроллер

[HttpPost, DisableRequestSizeLimit]
    public ActionResult UploadFile(string email)
    {

        var files = Request.Form.Files;
        foreach (var file in files)
        {
            string folderName = "BasteDocuments";
            string webRootPath = _hostingEnvironment.WebRootPath;
            string newPath = Path.Combine(webRootPath, folderName);
            if (!Directory.Exists(newPath))
            {
                Directory.CreateDirectory(newPath);
            }
            if (file.Length > 0)
            {
                string fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
                string newname = email + "_" + fileName;
                string fullPath = Path.Combine(newPath, newname);
                using (var stream = new FileStream(fullPath, FileMode.Create))
                {
                    file.CopyTo(stream);
                }
            }

        }
        return Ok();
    }

1 Ответ

0 голосов
/ 28 февраля 2019

Если this.files является экземпляром FormData, вы можете установить любое поле, которое вам нужно.Например,

upload () {
  this.files.set('email', this.profile.email)

  axios.post('/api/upload', this.files)...

Я не очень хорошо знаю .NET MVC, но это добавит email в качестве параметра формы в запросе.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...