使用VUE+iView+.Net Core上傳圖片的方法示例
我們直接進(jìn)入主題,使用VS2017開發(fā)工具
首先要?jiǎng)?chuàng)建一個(gè)WebApi項(xiàng)目,創(chuàng)建完之后,在wwwroot文件下,創(chuàng)建一個(gè)文件夾 名字可以隨意起 我這里呢就叫做Upload了

ok ! 然后我們?cè)賱?chuàng)建一個(gè)控制器 IndexController 代碼如下
要知道上傳圖片都是通過HTTP去請(qǐng)求,服務(wù)端從request中讀取
public class PicData
{
public string Msg { get; set; }
}
[HttpPost]
public async Task<bool> InsertPicture([FromServices]IHostingEnvironment environment)
{
var data = new PicData();
string path = string.Empty;
var files = Request.Form.Files;
if (files == null || files.Count() <= 0)
{
data.Msg = "請(qǐng)選擇上傳的文件。";
return false;
}
//格式限制
var allowType = new string[] {"image/jpg", "image/png", "image/jpeg"};
if (files.Any(c => allowType.Contains(c.ContentType)))
{
if (files.Sum(c => c.Length) <= 1024 * 1024 * 4)
{
foreach (var file in files)
{
string strpath = Path.Combine("Upload", DateTime.Now.ToString("MMddHHmmss") + file.FileName);
path = Path.Combine(environment.WebRootPath, strpath);
using (var stream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
await file.CopyToAsync(stream);
}
}
data.Msg = "上傳成功";
return true;
}
else
{
data.Msg = "圖片過大";
return false;
}
}
else
{
data.Msg = "圖片格式錯(cuò)誤";
return false;
}
}
注意一下這段代碼
string strpath = Path.Combine("Upload", DateTime.Now.ToString("MMddHHmmss") + file.FileName);
在wwwroot下創(chuàng)建的文件夾,要將Upload替換成你的文件夾名稱
然后這還沒有完,還要做跨域,要在Startup.cs文件中去改
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddCors(options =>
{
options.AddPolicy("hehe", p => p.AllowAnyMethod()// 允許任何方法 GET,POST,PUT,DELETE, OPTIONS
.AllowAnyHeader() // 允許任何請(qǐng)求頭
.AllowAnyOrigin() // 允許任何地址
);
});
}
ConfigureServices方法,然后還有Configure方法
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseStaticFiles(); app.UseCors("hehe");
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseMvc();
}
ok完成了,
然后我們就要去創(chuàng)建Vue項(xiàng)目了,
使用npm創(chuàng)建vue項(xiàng)目,vue init webpack file 我們跳過創(chuàng)建過程
使用npm 引用iview 然后在vue項(xiàng)目中的main.js中引用
import iView from 'iview';
import 'iview/dist/styles/iview.css';
import locale from 'iview/dist/locale/en-US';
Vue.use(iView, { locale });
ok,然后我們就在app.vue里面寫代碼
<template>
<div id="app">
<Upload action="http://localhost:53688/api/Index">
<Button icon="ios-cloud-upload-outline">Upload files</Button>
</Upload>
</div>
</template>
action:就是api的地址
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
詳解Vue中Computed與watch的用法與區(qū)別
這篇文章主要介紹了Vue中computed和watch的使用與區(qū)別,文中通過示例為大家進(jìn)行了詳細(xì)講解,對(duì)Vue感興趣的同學(xué),可以學(xué)習(xí)一下2022-04-04
vue之a(chǎn)-table中實(shí)現(xiàn)清空選中的數(shù)據(jù)
今天小編就為大家分享一篇vue之a(chǎn)-table中實(shí)現(xiàn)清空選中的數(shù)據(jù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-11-11
vue使用elementUI分頁(yè)如何實(shí)現(xiàn)切換頁(yè)面時(shí)返回頁(yè)面頂部
這篇文章主要介紹了vue使用elementUI分頁(yè)如何實(shí)現(xiàn)切換頁(yè)面時(shí)返回頁(yè)面頂部,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08
關(guān)于vant的日歷組件,在iPhonex上可選日期空白
這篇文章主要介紹了關(guān)于vant的日歷組件,在iPhonex上可選日期空白,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04
vue路由切換時(shí)取消之前的所有請(qǐng)求操作
這篇文章主要介紹了vue路由切換時(shí)取消之前的所有請(qǐng)求操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-09-09
使用Vue3-Ace-Editor如何在Vue3項(xiàng)目中集成代碼編輯器
這篇文章主要介紹了使用Vue3-Ace-Editor如何在Vue3項(xiàng)目中集成代碼編輯器,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08

