当前位置: 移动技术网 > IT编程>脚本编程>vue.js > vue.js异步上传文件前后端实现代码

vue.js异步上传文件前后端实现代码

2017年12月12日  | 移动技术网IT编程  | 我要评论

红卐会,毕业设计ppt怎么做,宋彬彬

本文实例为大家分享了vue.js异步上传文件的具体代码,供大家参考,具体内容如下

上传文件前端代码如下:

<!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)
      {
        //设置文件名
        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);
    }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网