溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

vue.js異步上傳文件前后端實現(xiàn)代碼

發(fā)布時間:2020-10-20 01:31:23 來源:腳本之家 閱讀:191 作者:qkabcd 欄目:web開發(fā)

本文實例為大家分享了vue.js異步上傳文件的具體代碼,供大家參考,具體內(nèi)容如下

上傳文件前端代碼如下:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  <title></title>
  <meta charset="utf-8" />
  <script src="../js/vue.js"></script>
  <script src="../js/vue-resource.js"></script>
  <script src="../asset/js/jquery.js"></script>

</head>
<body>
  <div id="app">
    <input type="file" @change="getFile($event)" /><button @click="upload">上傳</button>
    <div>{{ result }}</div>
    <div v-show="uping==1">正在上傳中</div>
  </div>

<script>
  new Vue({
    el: '#app',
    data: {
      upath: '',
      result: '',
      uping:0
    },
    methods: {
      upload: function () {
        //console.log(this.upath);
        var zipFormData = new FormData();
        zipFormData.append('filename', this.upath);//filename是鍵,file是值,就是要傳的文件,test.zip是要傳的文件名
        let config = { headers: { 'Content-Type': 'multipart/form-data' } };
        this.uping = 1;
        this.$http.post('http://localhost:42565/home/up', zipFormData,config).then(function (response) {
          console.log(response);
          console.log(response.data);
          console.log(response.bodyText);
          
          var resultobj = response.data;
          this.uping = 0;
          this.result = resultobj.msg;
        });
      },

      getFile: function (even) {
        this.upath = event.target.files[0];
      },
    }
  });
</script>
</body>
</html>

后端處理代碼如下asp.net mvc的:

public ActionResult Up()
    {
      string msg = string.Empty;
      string error = string.Empty;
      string result = string.Empty;
      string filePath = string.Empty;
      string fileNewName = string.Empty;
      var files = Request.Files;
      if (files.Count > 0)
      {
        //設(shè)置文件名
        fileNewName = DateTime.Now.ToString("yyyyMMddHHmmssff") + "_" + System.IO.Path.GetFileName(files[0].FileName);
        //保存文件
        files[0].SaveAs(Server.MapPath("~/Uploads/" + fileNewName));
        Thread.Sleep(10 * 1000);
      }
      return Json(new { msg = "上傳成功", newfilename = fileNewName }, JsonRequestBehavior.AllowGet);
    }

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI