【问题标题】:Sending JSON + File via Postman通过 Postman 发送 JSON + 文件
【发布时间】:2021-02-19 06:10:29
【问题描述】:

我有一个 Android 应用程序,它将以下多部分内容发布到我的应用程序。该请求包含 JSON 以及图像文件。

        AndroidNetworking.upload(baseUrl+"updateuserprofile")
            .addMultipartFile("UserImage",userImage)
            .setTag("UserImage")
            .addMultipartParameter("PhoneNumber",phone)
            .addMultipartParameter("UserId",userId)
            .addMultipartParameter("firstName",firstName)
            .addMultipartParameter("LastName",lastName)
            .setPriority(Priority.HIGH)
            .setOkHttpClient(okHttpClient)
            .build();

我的应用程序是一个 ASP .NET WebAPI 2.0 应用程序,我无法弄清楚如何使用 EditProfile 参数,而这个参数总是被证明是 NULL

public async Task<IHttpActionResult> EditProfile ([FromBody] EditProfile profile)
{
    // `profile` is populated if only JSON is sent.
    // `profile` is `NULL` if multi-part content is sent.
}

public class EditProfile
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }
    public string MobileNo { get; set; }
    public long UserId { get; set; }
    public string ImageString { get; set; }
    public HttpPostedFileBase PostedFile { get; set; }
}

任何建议将不胜感激。

【问题讨论】:

    标签: c# asp.net asp.net-web-api asp.net-core-webapi multipartform-data


    【解决方案1】:

    当您发送多部分内容时,它不是 json 而是表单数据。因此,您应该使用[FromFrom],而不是使用[FromBody]。 ([FromForm] 是 ASP.Net Core 的一个功能,因为你使用的是完整框架,所以它不适合你)

    public async Task<IHttpActionResult> EditProfile ([FromForm] EditProfile profile)
    {
        // `profile` will not be populated if JSON is sent.
        // `profile` will be populated if multi-part content is sent.
    }
    
    public class EditProfile
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Email { get; set; }
        public string MobileNo { get; set; }
        public long UserId { get; set; }
        public string ImageString { get; set; }
        public HttpPostedFileBase PostedFile { get; set; }
    }
    

    我相信 ASP.Net Web API(完整框架)不允许您将 HttpPostedFile 添加为模型的一部分。如果您发现PostedFile 之类的任何问题null。删除 HttpPostedFile 作为您的操作的参数。

    如果您需要接受不带文件的 Json 请求和带文件的多部分表单数据。您应该从参数中删除 [FromForm],并让框架为您处理。

    public async Task<IHttpActionResult> EditProfile (EditProfile profile, HttpPostedFileBase postedFile)
    {
        // `profile` should be populated with a JSON or multi-part content
        // `postedFile` will be  populated only if multi-part content is sent with a file.
    }
    
    public class EditProfile
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Email { get; set; }
        public string MobileNo { get; set; }
        public long UserId { get; set; }
        public string ImageString { get; set; }
        // REMOVE HttpPostedFileBase property
    }
    

    【讨论】:

    • 我尝试了这两种方法。 [FromForm] 需要对 AspNetCore 的依赖,这对我来说是不可能的。下一个带有HttpPostedFileBase 的示例会抛出InvalidOperationExceptionCan't bind multiple parameters ('editProfile' and 'postedFile') to the request's content
    • 对不起。第一个使用[FromForm] 仅在asp.net 核心上工作。我会更新答案以使其清楚。
    • 在第二个示例中,您是否从模型中删除了 HttpPostedFileBase?
    • 在模型中使用和不使用HttpPostedFileBase 进行尝试。现在的错误是:Can't bind multiple parameters ('editProfile' and 'postedFile') to the request's content.
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-07
    • 2016-12-24
    • 1970-01-01
    • 2019-09-12
    • 2018-10-15
    • 2016-01-30
    • 1970-01-01
    相关资源
    最近更新 更多