.net core 无法从Axios(vue nuxt)向.net core API发送文件
如果我尝试将文件从Vue发送到.net Core中的API端点,我将返回500个错误 我跟随了做这件事的教程,但他们似乎不适合这种设置 .net核心API:.net core 无法从Axios(vue nuxt)向.net core API发送文件,.net-core,vuejs2,axios,nuxt.js,.net Core,Vuejs2,Axios,Nuxt.js,如果我尝试将文件从Vue发送到.net Core中的API端点,我将返回500个错误 我跟随了做这件事的教程,但他们似乎不适合这种设置 .net核心API: [Route("api/[controller]")] [ApiController] public class FileUploadController : ControllerBase { [HttpPost("[Action]")] public string send
[Route("api/[controller]")]
[ApiController]
public class FileUploadController : ControllerBase
{
[HttpPost("[Action]")]
public string sendFiles([FromBody]FileUploadAPI file)
{
return "Yes!";
}
public class FileUploadAPI
{
public IFormFile File { get; set; }
}
}
Vue:
我想在API中接收我的文件
请求失败,状态代码为500您将收到404错误,因为您使用了错误的URL 您的操作名称是
sendFiles
(复数),因此正确的URL路径应该是/api/FileUpload/sendFiles
Axios能够作为多部分/表单数据
请求正确处理表单数据
。您不需要设置标题(无论如何都是不正确的),也不应该将数据
包装到对象中
let data=new FormData();
data.append('file',files[0]);//假设“文件”指的是文件列表
此.$axios.post('https://localhost:44352/api/FileUpload/sendFiles",数据)
.然后(…)
由于使用了错误的URL,您将收到404错误
您的操作名称是sendFiles
(复数),因此正确的URL路径应该是/api/FileUpload/sendFiles
Axios能够作为多部分/表单数据
请求正确处理表单数据
。您不需要设置标题(无论如何都是不正确的),也不应该将数据
包装到对象中
let data=new FormData();
data.append('file',files[0]);//假设“文件”指的是文件列表
此.$axios.post('https://localhost:44352/api/FileUpload/sendFiles",数据)
.然后(…)
以下示例代码片段可能对您有所帮助。在其中,我使用vuetify、vue upload组件和axios上传图像
<template lang="html">
<div class="imageUploader">
<!-- <v-card> -->
<!-- <div v-show="$refs.upload && $refs.upload.dropActive" class="drop-active"></div> -->
<div class="avatar-upload">
<div class="text-center p-2">
<div class="avatar-container">
<div class="no-image" v-if="files.length === 0 && file == ''">
<v-icon>cloud_upload</v-icon>
</div>
<template v-else>
<img :src="file" alt="">
</template>
</div>
</div>
<div class="text-center p-2">
<v-btn class="browse-btn" flat>
<file-upload
extensions="gif,jpg,jpeg,png,webp"
accept="image/png,image/gif,image/jpeg,image/webp"
name="avatar"
v-model="files"
@input="uploadImage"
ref="upload">
Choose File
</file-upload>
</v-btn>
</div>
</div>
<!-- </v-card> -->
</div>
</template>
<script>
import Cropper from 'cropperjs'
import VueUploadComponent from 'vue-upload-component'
//import axios from 'axios'
export default {
components: {
'file-upload': VueUploadComponent
},
props: ['order', 'imageURL'],
data() {
return {
dialog: false,
files: [],
edit: false,
cropper: false,
file: '',
}
},
mounted() {
if (this.imageURL) {
this.file = this.$baseURL+'document/downloadimage/' + this.imageURL
}
},
watch: {
imageURL() {
if (this.imageURL) {
this.file = this.$baseURL+'document/downloadimage/' + this.imageURL
}
},
},
methods: {
**uploadImage(file) {
let formData = new FormData();
formData.append('file', file[0].file);
axios.post(axios.defaults.baseURL + 'document/uploadimage', formData, {headers: {'Content-Type': 'multipart/form-data'}})
.then((response) => {
this.dialog = false
this.$emit('upload', {id: response.data.result[0].objectId, order: this.order})
this.file = this.$baseURL+'document/downloadimage/' + response.data.result[0].objectId
let reader = new FileReader()
reader.readAsDataURL(file[0].file)
reader.onload = () => {
let base64 = reader.result.split(',')[1]
this.$emit('base64', base64)
}
this.getDimensions(this.$baseURL+'document/downloadimage/' + response.data.result[0].objectId, (result) => {
this.$emit('dimensions', {width: result.width, height: result.height})
})
})
.catch((error) => {
console.log(error)
})
},**
getDimensions(url, callback) {
var img = new Image();
img.src = url
img.onload = function() {
var result = {width: this.width, height: this.height}
callback(result)
}
}
},
}
</script>
云端上传
选择文件
从“cropperjs”导入裁剪器
从“vue上载组件”导入VueUploadComponent
//从“axios”导入axios
导出默认值{
组成部分:{
“文件上载”:VueUploadComponent
},
道具:['order','imageURL'],
数据(){
返回{
对话:错,
文件:[],
编辑:false,
克罗珀:错,
文件:“”,
}
},
安装的(){
if(this.imageURL){
this.file=this.$baseURL+'document/downloadimage/'+this.imageURL
}
},
观察:{
imageURL(){
if(this.imageURL){
this.file=this.$baseURL+'document/downloadimage/'+this.imageURL
}
},
},
方法:{
**上传图像(文件){
设formData=new formData();
formData.append('file',文件[0].file);
post(axios.defaults.baseURL+'document/uploadimage',formData,{headers:{'Content-Type':'multipart/formData'})
。然后((响应)=>{
this.dialog=false
this.$emit('upload',{id:response.data.result[0].objectId,order:this.order})
this.file=this.$baseURL+'document/downloadimage/'+response.data.result[0]。objectId
let reader=new FileReader()
reader.readAsDataURL(文件[0]。文件)
reader.onload=()=>{
让base64=reader.result.split(',')[1]
此.$emit('base64',base64)
}
this.getDimensions(this.$baseURL+'document/downloadimage/'+response.data.result[0]。objectId,(result)=>{
这是。$emit('dimensions',{width:result.width,height:result.height})
})
})
.catch((错误)=>{
console.log(错误)
})
},**
getDimensions(url、回调){
var img=新图像();
img.src=url
img.onload=函数(){
var result={width:this.width,height:this.height}
回调(结果)
}
}
},
}
以下示例代码片段可能对您有所帮助。在其中,我使用vuetify、vue upload组件和axios上传图像
<template lang="html">
<div class="imageUploader">
<!-- <v-card> -->
<!-- <div v-show="$refs.upload && $refs.upload.dropActive" class="drop-active"></div> -->
<div class="avatar-upload">
<div class="text-center p-2">
<div class="avatar-container">
<div class="no-image" v-if="files.length === 0 && file == ''">
<v-icon>cloud_upload</v-icon>
</div>
<template v-else>
<img :src="file" alt="">
</template>
</div>
</div>
<div class="text-center p-2">
<v-btn class="browse-btn" flat>
<file-upload
extensions="gif,jpg,jpeg,png,webp"
accept="image/png,image/gif,image/jpeg,image/webp"
name="avatar"
v-model="files"
@input="uploadImage"
ref="upload">
Choose File
</file-upload>
</v-btn>
</div>
</div>
<!-- </v-card> -->
</div>
</template>
<script>
import Cropper from 'cropperjs'
import VueUploadComponent from 'vue-upload-component'
//import axios from 'axios'
export default {
components: {
'file-upload': VueUploadComponent
},
props: ['order', 'imageURL'],
data() {
return {
dialog: false,
files: [],
edit: false,
cropper: false,
file: '',
}
},
mounted() {
if (this.imageURL) {
this.file = this.$baseURL+'document/downloadimage/' + this.imageURL
}
},
watch: {
imageURL() {
if (this.imageURL) {
this.file = this.$baseURL+'document/downloadimage/' + this.imageURL
}
},
},
methods: {
**uploadImage(file) {
let formData = new FormData();
formData.append('file', file[0].file);
axios.post(axios.defaults.baseURL + 'document/uploadimage', formData, {headers: {'Content-Type': 'multipart/form-data'}})
.then((response) => {
this.dialog = false
this.$emit('upload', {id: response.data.result[0].objectId, order: this.order})
this.file = this.$baseURL+'document/downloadimage/' + response.data.result[0].objectId
let reader = new FileReader()
reader.readAsDataURL(file[0].file)
reader.onload = () => {
let base64 = reader.result.split(',')[1]
this.$emit('base64', base64)
}
this.getDimensions(this.$baseURL+'document/downloadimage/' + response.data.result[0].objectId, (result) => {
this.$emit('dimensions', {width: result.width, height: result.height})
})
})
.catch((error) => {
console.log(error)
})
},**
getDimensions(url, callback) {
var img = new Image();
img.src = url
img.onload = function() {
var result = {width: this.width, height: this.height}
callback(result)
}
}
},
}
</script>
云端上传
选择文件
从“cropperjs”导入裁剪器
从“vue上载组件”导入VueUploadComponent
//从“axios”导入axios
导出默认值{
组成部分:{
“文件上载”:VueUploadComponent
},
道具:['order','imageURL'],
数据(){
返回{
对话:错,
文件:[],
编辑:false,
克罗珀:错,
文件:“”,
}
},
安装的(){
if(this.imageURL){
this.file=this.$baseURL+'document/downloadimage/'+this.imageURL
}
},
观察:{
imageURL(){
if(this.imageURL){
this.file=this.$baseURL+'document/downloadimage/'+this.imageURL
}
},
},
方法:{
**上传图像(文件){
设formData=new formData();
formData.append('file',文件[0].file);
post(axios.defaults.baseURL+'document/uploadimage',formData,{headers:{'Content-Type':'multipart/formData'})
。然后((响应)=>{
this.dialog=false
this.$emit('upload',{id:response.data.result[0].objectId,order:this.order})
this.file=this.$baseURL+'document/downloadimage/'+response.data.result[0]。objectId
let reader=new FileReader()
reader.readAsDataURL(文件[0]。文件)
reader.onload=()=>{
让base64=reader.result.split(',')[1]
此.$emit('base64',base64)
}
this.getDimensions(this.$baseURL+'document/downloadimage/'+response.data.result[0]。objectId,(result)=>{
这是。$emit('dimensions',{width:result.width,height:result.height})
})
})
.catch((错误)=>{
console.log(错误)
})
},**
getDimensions(url、回调){
var img=新图像();
img.src=url
img.onload=