【问题标题】:send lengthy base64 string to webapi向 webapi 发送冗长的 base64 字符串
【发布时间】:2020-11-28 08:36:18
【问题描述】:

在这里,我使用我的 Angular js 中的值参数向 webapi 控制器发送发布请求, 包含base64字符串值的参数之一,

$scope.upload = function(Id, formdata) {
            debugger;
            if ($scope.logoattachments[0].fileSize != '' && $scope.logoattachments[0].fileSize != null && $scope.logoattachments[0].fileSize != undefined) {
                var data = formdata //new FormData($("form")[0]);
                var urls =
                    $localStorage.apiURL +
                    "/LogoUpload?tcoid=" +
                    Id + "&documentName=" +
                    $scope.logoattachments[0].documentName +
                    "&fileName=" + $scope.logoattachments[0].fileName +
                    "&fileType=" + $scope.logoattachments[0].fileType +
                    "&fileSize=" + $scope.logoattachments[0].fileSize +
                    "&fileBaseContent=" + $scope.fileBaseContent;
                $.ajax({
                    type: "POST",
                    url: urls,
                    enctype: "multipart/form-data",
                    contentType: false,
                    processData: false,
                    cache: false,
                    data: data,
                    success: function(data, textStatus, xhr) {
                        $("p").text(data);
                        $scope.showlodebar = false;
                        $location.url("/update/" + tcoId);
                    },
                    error: function(XMLHttpRequest, textStatus, errorThrown) {
                        toastr.error(textStatus + ": " + errorThrown);
                    },
                });
            }
        };

我的控制器代码

[Route("api/LogoUpload")]
    [HttpPost()]
    public string PostAction([FromUri] int id,string documentName,string fileName,string fileType,int fileSize,string fileBaseContent)
    {
        try
        {
            var userIdentity = RequestContext.Principal.Identity;
            LogoAttachment model = new LogoAttachment();
            model.documentName = documentName;
            model.fileName = fileName;
            model.fileType = fileType;
            model.fileSize = fileSize;
            model.fileBaseContent = "";
            model.createdBy = "Super Admin";
            model.createdAt = DateTime.Now;
            model.updatedBy = "Super Admin";//userIdentity.Name;
            model.updatedAt = DateTime.Now;
            model.isActive = true;
            model.tcoid = tcoid;
            context.LogoAttachments.Add(model);
            context.SaveChanges();
            context.Dispose();
            if (iUploadedCnt > 0)
            {
                return iUploadedCnt + " Files Uploaded Successfully";
            }
            else
            {
                return "Upload Failed";
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
        }
        return "";
    }
   

当我传递请求时,它显示一个 http 错误,

有人知道如何将base64字符串传递给控制器​​的解决方案吗?,请说出你的答案,

提前致谢

【问题讨论】:

  • Base 64 字符串作为参数传递,是文件内容吗?
  • 是的,它是@AdityaSingh
  • 不使用 FromUri,而是使用 FromBody 发送 Base64 内容。因为 url 有一个最大长度,并且当它超过请求失败时。由于 Base64 的长度较长,建议使用 FromBody 传递包装在模型类中的所有参数。

标签: asp.net angularjs asp.net-web-api angularjs-directive base64


【解决方案1】:

除了将文件详细信息作为查询字符串参数传递之外,您还可以将文件发布到 Web api 控制器,在控制器中,您可以从 httpcontext 中获取文件并对其进行解析以供进一步使用,请参见下文:-

var data = new formdata();
data .append('uploadedFile', document.getElementById("inputFile").files[0]);

在控制器中,您可以使用以下方法检索文件:-

var httpPostedFile = HttpContext.Current.Request.Files["uploadedFile"];

这里假设你的 web api 是基于 .net 框架的,请不要在发布请求时设置 contentType。

【讨论】:

    猜你喜欢
    • 2019-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-22
    • 1970-01-01
    相关资源
    最近更新 更多