微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何将文件发布到 WebAPI 客户端? AgentPortal 上传端点下划线为 webAPI 客户端端点eCommUtility 客户端端点下划线是我将文件发布到 API 端点的方式:ecommUtility API 端点:

如何解决如何将文件发布到 WebAPI 客户端? AgentPortal 上传端点下划线为 webAPI 客户端端点eCommUtility 客户端端点下划线是我将文件发布到 API 端点的方式:ecommUtility API 端点:

我正在创建一个文件上传到某个位置的 WebAPI 项目。我有两个项目方案,一个叫AgentPortal,另一个叫eCommUtility。

这个想法是让 eCommUtility 在服务器上运行,AgentPortal 会将文件发布到 eCommUtility 上名为 UploadDocument 的端点,端点会将文件上传到指定位置。

在 AgentPortal 中,有一个 actionResult 方法,它以 HttpPostedFile 作为参数,理想的解决方案是将文件发布到 webAPI 客户端上的端点。但是,这是我目前拥有的:

AgentPortal 上传端点(下划线为 webAPI 客户端端点)

enter image description here

eCommUtility 客户端端点(下划线是我将文件发布到 API 端点的方式:

enter image description here

ecommUtility API 端点:

[HttpPost]
[Route("api/UploadDocuments/UploadDocument")]
public async Task<HttpResponseMessage> UploadDocument()
{
    #region Variables

    string destinationPath = ConfigurationManager.AppSettings["fileUploadpath"];
    string message;
    HttpResponseMessage result = null;
    HttpRequest httpRequest = HttpContext.Current.Request;
    UploadDocumentModel model = new UploadDocumentModel();

    #endregion

    if (httpRequest.Files.Count > 0)
    {
        foreach (string file in httpRequest.Files)
        {
            var postedFile = httpRequest.Files[file];

            if (postedFile.FileName.Equals("TaxID")) // Check for the posted TaxID
            {
                var fileLen = postedFile.ContentLength;
                byte[] input = new byte[fileLen];

                var stream = postedFile.InputStream;
                stream.Read(input,fileLen);

                model.TaxID = Encoding.UTF8.GetString(input,input.Length);
            }
            else
            {
                model.FileName = postedFile.FileName;
                model.ReceivedDate = DateTime.Now.ToString();

                try
                {
                    string[] extension = postedFile.FileName.Split('.');

                    if (extension[extension.Length - 1].ToString() == "exe" ||
                        extension[extension.Length - 1].ToString() == "bat" ||
                        extension[extension.Length - 1].ToString() == "cmd")
                    {
                        model.ResponseCode = HttpStatusCode.BadRequest;
                        model.RequestMessage = postedFile.FileName + " is an invalid file type. Only PDF or image formats allowed.";
                        result = Request.CreateResponse(HttpStatusCode.BadRequest,model);
                    }
                    else
                    {
                        var filePath = destinationPath + "\\" + postedFile.FileName;
                        postedFile.SaveAs(filePath);


                        UploadDocumentProvider.UploadedFile(model.TaxID,model.FileName,model.ReceivedDate);
                        model.FileList = UploadDocumentProvider.GetAppUploaddisplay(model.TaxID);
                        model.ResponseCode = HttpStatusCode.Created;
                        model.RequestMessage = "You have just successfully uploaded a file called " + postedFile.FileName +
                                 ". Please check the Policy Inquiry area in the next couple of days for status information on this application.";

                        result = Request.CreateResponse<UploadDocumentModel>(HttpStatusCode.Created,model);
                    }


                }
                catch (IOException fe)
                {
                    model.RequestMessage = "An error occurred when uploading your file: " + postedFile.FileName;
                    model.ResponseCode = HttpStatusCode.BadRequest;
                    model.Exception = fe;
                    result = Request.CreateResponse(HttpStatusCode.BadRequest,model);
                }
                catch (Exception ex)
                {
                    model.RequestMessage = "An error occurred when uploading your file: " + postedFile.FileName;
                    model.ResponseCode = HttpStatusCode.BadRequest;
                    model.Exception = ex;
                    result = Request.CreateResponse(HttpStatusCode.BadRequest,model);
                }
            }
        }
    }
    else
    {
        result = Request.CreateResponse(HttpStatusCode.BadRequest);
    }
    return result;
}

在这仅在我手动设置文件path 时有效,但我需要将文件从 AgentPortal 一直发送到 API 端点。我不确定我是否应该以某种方式序列化文件然后发布它或如何处理这个。有人能解释一下这个问题吗?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。