C# 将文件从WinForms上载到.Net Core 3.1 API

C# 将文件从WinForms上载到.Net Core 3.1 API,c#,.net,C#,.net,这段代码在.Net Core 2.2上已经运行了6个月,但当我们“升级”到.Net Core 3.1时就不再有效了。该文件在服务器上接收,但其他参数始终为空 客户端代码: public async Task<bool> UploadFile(string sourcePath, string destinationPath, bool createPath, bool overwriteFile) { bool status = false;

这段代码在.Net Core 2.2上已经运行了6个月,但当我们“升级”到.Net Core 3.1时就不再有效了。该文件在服务器上接收,但其他参数始终为空

客户端代码:

    public async Task<bool> UploadFile(string sourcePath, string destinationPath, bool createPath, bool overwriteFile)
    {
        bool status = false;

        try
        {
            MultipartFormDataContent multipart = new MultipartFormDataContent();
            multipart.Add(new StringContent(destinationPath), "destinationPath");
            multipart.Add(new StringContent(createPath.ToString()), "createPath");
            multipart.Add(new StringContent(overwriteFile.ToString()), "overwriteFile");
            multipart.Add(new ByteArrayContent(File.ReadAllBytes(sourcePath)), "files", "files");

            // Select the API to call
            string path = $"UploadFile/{multipart}";

            // Make request and get response
            HttpResponseMessage response = await restClient.PostAsync(path, multipart);
            if (response.IsSuccessStatusCode)
            {
                string jsonResult = await response.Content.ReadAsStringAsync();
                if (jsonResult.Contains("true"))
                    status = true;
            }
        }
        catch (Exception ex)
        {
            appLog.WriteError(ex.Message);
        }
public异步任务上载文件(字符串sourcePath、字符串destinationPath、bool createPath、bool overwriteFile)
{
布尔状态=假;
尝试
{
MultipartFormDataContent multipart=新的MultipartFormDataContent();
添加(新的StringContent(destinationPath),“destinationPath”);
添加(新的StringContent(createPath.ToString()),“createPath”);
添加(新的StringContent(overwriteFile.ToString()),“overwriteFile”);
添加(新的ByteArrayContent(File.ReadAllBytes(sourcePath)),“files”,“files”);
//选择要调用的API
字符串路径=$“UploadFile/{multipart}”;
//发出请求并得到响应
HttpResponseMessage response=等待restClient.PostAsync(路径,多部分);
if(响应。IsSuccessStatusCode)
{
字符串jsonResult=await response.Content.ReadAsStringAsync();
if(jsonResult.Contains(“true”))
状态=真;
}
}
捕获(例外情况除外)
{
appLog.WriteError(例如消息);
}
API代码:

    [HttpPost("UploadFile/{multipart}", Name = "UploadFile")]
    [RequestSizeLimit(40000000)] // Max body size (upload file size)
    public async Task<ActionResult<bool>> UploadFile(List<IFormFile> files, string destinationPath, bool createPath, bool overwriteFile)

    {
        // server side code here
    }
[HttpPost(“UploadFile/{multipart}”,Name=“UploadFile”)]
[RequestSizeLimit(40000000)]//最大正文大小(上载文件大小)
公共异步任务上载文件(列表文件、字符串destinationPath、bool createPath、bool overwriteFile)
{
//这里是服务器端代码
}

控制台上没有明显的错误。所有其他API都可以正常工作,只有这个使用MultipartFormDataContent代码的API不再正常工作。

我非常确定这样的多个参数不再被允许。幸运的是,它通常在确定对象方面非常聪明

与其传递多个参数,不如用属性与现有参数匹配的对象替换这些参数。它应该可以在不更改客户端的情况下工作

public class PostBodyMessage
{
 public List<IFormFile> files {get; set;}
 public string destinationPath {get; set;}
 public bool createPath {get; set;}
 public bool overwriteFile {get; set;}
}

[HttpPost("UploadFile/{multipart}", Name = "UploadFile")]
[RequestSizeLimit(40000000)] // Max body size (upload file size)
public async Task<ActionResult<bool>> UploadFile(PostBodyMessage message)

{
    // server side code here
}
公共类PostBodyMessage
{
公共列表文件{get;set;}
公共字符串目标路径{get;set;}
公共bool创建路径{get;set;}
公共bool覆盖文件{get;set;}
}
[HttpPost(“UploadFile/{multipart}”,Name=“UploadFile”)]
[RequestSizeLimit(40000000)]//最大正文大小(上载文件大小)
公共异步任务上载文件(PostBodyMessage)
{
//这里是服务器端代码
}

这应该是可行的。

。如果您决定升级到较新版本,NET Core将牺牲与API更改的向后兼容性。1.如何将文件分配给IFormFile 2.如何通过:multipart.add(message)添加复杂类型;来自客户端?如果这是有效的,你不应该改变它…也许我完全错了。MultiPartFormData设计用于收集web表单数据并将其发布到API。这不完全是我正在做的。我有一个服务可以获取现有文件,设置参数,并发布到API。也许还有另一种方法可以完成同样的事情?我可以对文件进行base64编码,并将所有内容作为json对象传递,但我对base64编码大型zip文件的想法并不感到兴奋,只是为了逃避这个挑战。Austin,我不理解你的观点。旧代码在从2.2升级到.Net Core 3.1后不再有效。我还应该指出t客户端是一个.Net Framework 4.7.2应用程序,与.Net Core 3.1 web API进行通信。在原始代码中,我将文件内容分配给IFormFile,如下所示:file.ReadAllBytes(sourcePath);这样做现在显示一个错误“无法隐式转换类型byte[]到Microsoft.AspNetCore.Http.ifformfile。那么,将文件分配到ifformfile的正确方法是什么?