Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# .net core无法将当前JSON数组反序列化为类型';Microsoft.AspNetCore.Http.ifformfile';_C#_Json_Asp.net Core - Fatal编程技术网

C# .net core无法将当前JSON数组反序列化为类型';Microsoft.AspNetCore.Http.ifformfile';

C# .net core无法将当前JSON数组反序列化为类型';Microsoft.AspNetCore.Http.ifformfile';,c#,json,asp.net-core,C#,Json,Asp.net Core,我正在尝试上载图片,通过前端发送图片(axios请求),然后服务器返回此错误 无法将当前JSON数组反序列化为类型 “Microsoft.AspNetCore.Http.ifformfile”,因为该类型需要JSON 对象以正确反序列化。要修复此错误,请更改 JSON到JSON对象(例如{“name”:“value”})或更改 将类型反序列化为数组或实现集合的类型 接口(如ICollection、IList)类似列表,可以 从JSON数组反序列化。还可以添加JsonArrayAttribute

我正在尝试上载图片,通过前端发送图片(axios请求),然后服务器返回此错误

无法将当前JSON数组反序列化为类型 “Microsoft.AspNetCore.Http.ifformfile”,因为该类型需要JSON 对象以正确反序列化。要修复此错误,请更改 JSON到JSON对象(例如{“name”:“value”})或更改 将类型反序列化为数组或实现集合的类型 接口(如ICollection、IList)类似列表,可以 从JSON数组反序列化。还可以添加JsonArrayAttribute 类型,以强制它从JSON数组反序列化。路径“文件”, 第1行,位置339。”

当我试图发布formdata时,它没有提交

编辑1:我发现问题在哪里, formData发送空文件对象,如下所示

formdata.get('file') // '[Object object]'

这不是对您问题的直接回答,但我发现使用base64编码时,通过ajax发送文件通常要容易得多。下面是一个小示例,介绍如何实现这一点:

//client side
//this function converts a file object to a base64 string
const toBase64 = file => new Promise((resolve, reject) => {
   const reader = new FileReader();
   reader.readAsDataURL(file);
   reader.onload = () => resolve(reader.result);
   reader.onerror = error => reject(error);
});

document.getElementById("upload").onclick = async () => {
    const file = document.getElementById('myfile').files[0];
    const base64 = await toBase64(file);
    const response = await fetch('/home/upload', {
        method: 'post',
        headers: {
            'Content-Type': 'application/json'
        },
        body: JSON.stringify( {base64 /*add any additional values you need here */})
})
    const json = await response.json();
}

//server side
public class UploadData
{
    public string Base64Data { get; set; }
    //any additional properties you need here....
}

[HttpPost]
public IActionResult Upload([FromBody]UploadData data)
{
    var base64 = data.Base64Data.Substring(data.Base64Data.IndexOf(",") + 1); //bit of a hack, need to remove the additional part of the base64 string that JS generates but that .net doesn't like
    var bytes = Convert.FromBase64String(base64);
    //bytes is now available to do whatever you need with
    return Json(someObjectYouWantToReturn);
}

希望这段代码能够成为一个很好的起点,帮助您找到需要的地方。同样,base64可能不是您想要的,但我经常使用这种方法,我发现它很容易亲自处理。

不是对您问题的直接回答,但我发现使用base64编码时,通过ajax发送文件通常要容易得多。H以下是一个小示例,说明如何实现这一点:

//client side
//this function converts a file object to a base64 string
const toBase64 = file => new Promise((resolve, reject) => {
   const reader = new FileReader();
   reader.readAsDataURL(file);
   reader.onload = () => resolve(reader.result);
   reader.onerror = error => reject(error);
});

document.getElementById("upload").onclick = async () => {
    const file = document.getElementById('myfile').files[0];
    const base64 = await toBase64(file);
    const response = await fetch('/home/upload', {
        method: 'post',
        headers: {
            'Content-Type': 'application/json'
        },
        body: JSON.stringify( {base64 /*add any additional values you need here */})
})
    const json = await response.json();
}

//server side
public class UploadData
{
    public string Base64Data { get; set; }
    //any additional properties you need here....
}

[HttpPost]
public IActionResult Upload([FromBody]UploadData data)
{
    var base64 = data.Base64Data.Substring(data.Base64Data.IndexOf(",") + 1); //bit of a hack, need to remove the additional part of the base64 string that JS generates but that .net doesn't like
    var bytes = Convert.FromBase64String(base64);
    //bytes is now available to do whatever you need with
    return Json(someObjectYouWantToReturn);
}

希望这段代码能成为一个很好的起点,让您达到您需要的目的。同样,base64可能不是您想要的,但我经常使用这种方法,我发现它很容易亲自处理。

首先尝试从postman或任何api测试软件中访问该api,然后使用前端语言使用该api。我如何测试POSIng postman上的图像文件或其他什么?您能显示正在上载文件的axios代码吗?从错误中,我猜您试图将其发布为JSON,并且应该将其发布为多部分上载。当我尝试多部分表单数据的内容类型时,我遇到此错误“无法读取请求表单”。缺少内容类型边界。“在postman中测试首先尝试从postman或任何api测试软件中访问该api,然后移动以使用前端语言使用该api。我如何在postman或其他软件上测试发布图像文件?您能否显示正在上载该文件的axios代码?”?从这个错误中,我猜您试图将其发布为JSON,并且应该将其发布为多部分上传。当我尝试多部分表单数据的内容类型时,我得到了这个错误“无法读取请求表单。缺少内容类型边界。”在postman中测试
//client side
//this function converts a file object to a base64 string
const toBase64 = file => new Promise((resolve, reject) => {
   const reader = new FileReader();
   reader.readAsDataURL(file);
   reader.onload = () => resolve(reader.result);
   reader.onerror = error => reject(error);
});

document.getElementById("upload").onclick = async () => {
    const file = document.getElementById('myfile').files[0];
    const base64 = await toBase64(file);
    const response = await fetch('/home/upload', {
        method: 'post',
        headers: {
            'Content-Type': 'application/json'
        },
        body: JSON.stringify( {base64 /*add any additional values you need here */})
})
    const json = await response.json();
}

//server side
public class UploadData
{
    public string Base64Data { get; set; }
    //any additional properties you need here....
}

[HttpPost]
public IActionResult Upload([FromBody]UploadData data)
{
    var base64 = data.Base64Data.Substring(data.Base64Data.IndexOf(",") + 1); //bit of a hack, need to remove the additional part of the base64 string that JS generates but that .net doesn't like
    var bytes = Convert.FromBase64String(base64);
    //bytes is now available to do whatever you need with
    return Json(someObjectYouWantToReturn);
}