vue.js異步上傳文件前后端實(shí)現(xiàn)代碼
本文實(shí)例為大家分享了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)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue 實(shí)現(xiàn)在函數(shù)中觸發(fā)路由跳轉(zhuǎn)的示例
今天小編就為大家分享一篇vue 實(shí)現(xiàn)在函數(shù)中觸發(fā)路由跳轉(zhuǎn)的示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-09-09
Vue使用.sync 實(shí)現(xiàn)父子組件的雙向綁定數(shù)據(jù)問題
這篇文章主要介紹了Vue使用.sync 實(shí)現(xiàn)父子組件的雙向綁定數(shù)據(jù),需要的朋友可以參考下2019-04-04
vuex中數(shù)據(jù)持久化插件vuex-persistedstate使用詳解
這篇文章主要介紹了vuex中數(shù)據(jù)持久化插件vuex-persistedstate使用詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
vue實(shí)現(xiàn)隨機(jī)驗(yàn)證碼功能的實(shí)例代碼
這篇文章主要介紹了vue實(shí)現(xiàn)隨機(jī)驗(yàn)證碼功能的實(shí)例代碼,代碼簡單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-04-04
用vite搭建vue3應(yīng)用的實(shí)現(xiàn)方法
這篇文章主要介紹了用vite搭建vue3應(yīng)用的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02

