gpt4 book ai didi

c# - 我可以从 Winforms 应用程序同时将文件和模型上传到 MVC 4 操作吗?

转载 作者:太空宇宙 更新时间:2023-11-03 13:36:44 25 4
gpt4 key购买 nike

我需要将签名板集成到 Intranet (MVC4) 应用程序中,允许人们将电子签名应用于系统生成的文档。不幸的是,我得到的签名板只有一个 COM/ActiveX API,所以我编写了一个简短的 Windows 窗体应用程序,允许用户捕获签名并将其上传到服务器。上传时,我需要 MVC4 操作将签名图像与 Windows 窗体请求发送的指定文档实体相关联。所以,假设我有这个模型:

public class DocumentToSign { 
public int DocumentId { get; set; }
public int DocumentTypeId { get; set; }
}

然后我有这个 Action 来接收上传的图片:

[HttpPost]
public ActionResult UploadSignature(DocumentToSign doc, HttpPostedFileBase signature)
{
//do stuff and catch errors to report back to winforms app
return Json(new {Success = true, Error = string.Empty});
}

然后是上传图片的代码:

var doc = new DocumentToSign{ DocumentId = _theId, DocumentTypeId = _theType };
var fileName = SaveTheSignature();
var url = GetTheUrl();
using(var request = new WebClient())
{
request.Headers.Add("enctype", "multipart/form-data");
foreach(var prop in doc.GetType().GetProperties())
{
request.QueryString.Add(prop.Name, Convert.ToString(prop.GetValue(doc, null)));
}
var responseBytes = request.UploadFile(url, fileName);
//deserialize resulting Json, etc.
}

模型联编程序似乎毫无问题地选择了 DocumentToSign 类,但是 HttpPostedFileBase 始终为 null。我知道我需要以某种方式告诉模型联编程序上传的图像是操作中的 signature 参数,但我不知道该怎么做。我尝试将 UploadValuesNameValueCollection 一起使用,但 NameValueCollection 只允许该值为 string,因此图像(即使作为 byte[])也不能成为其中的一部分。

是否可以从实际 MVC4 应用程序外部将文件和模型上传到同一操作?我应该使用 HttpPostedFileBase 以外的东西吗?除了 WebClient 之外?我很茫然。

最佳答案

var responseBytes = request.UploadFile(url, fileName); 没有以 Controller 期望的格式发送文件。HttpPostedFileBase 使用 multipart/form-data POST 请求。但是 WebClient.UploadFile 不发送多部分请求,它将文件内容作为请求主体发送,没有其他信息。您可以通过调用 Request.SaveAs(filename, false); 来保存文件或者您必须更改发送文件的方式。但我认为 WebClient 不支持发送多部分请求。

关于c# - 我可以从 Winforms 应用程序同时将文件和模型上传到 MVC 4 操作吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18450162/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com