用Windows Phone Background Transfer Service上传到MVC3网站以宁静的方式上传
-
26-10-2019 - |
题
我需要我的Windows Phone应用程序能够使用MANGO中可用的BackgroundTransFerservice将音频文件上传到我的MVC3站点。
作为一种可能的解决方案,我可以:
将路线映射到我的控制器:
public override void RegisterArea(AreaRegistrationContext context) { context.MapRoute( "SingleAudioFile", "Api/Audio/Recieve", new { controller = "AudioFiles", action = "Recieve" } ); }
在控制器中,采取措施
[HttpPost] public JsonResult Recieve(byte[] audio) { // saving and status report logic here }
我的问题是: :如何设置系统将文件从Windows Phone上传到一个 Recieve
动作 audio
字节[]参数?
在电话上,数据正在上传以下方式:
BackgroundTransferRequest btr = new BackgroundTransferRequest (new Uri
(siteUrl + "Api/Audio/Recieve",UriKind.Absolute));
btr.TransferPreferences = TransferPreferences.AllowBattery;
btr.Method = "POST";
btr.UploadLocation = new Uri("/" + Transfers + "/" + isoAudioFileName, UriKind.Relative);
Microsoft.Phone.BackgroundTransfer.BackgroundTransferService.Add(btr);
解决方案
我不太确定背景转移用于发送文件的协议是什么协议,但是如果将缓冲区直接写入邮政请求的正文,则可以使用自定义模型粘合剂直接从请求流中读取:
public class BTModelBinder : IModelBinder
{
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
using (var ms = new MemoryStream())
{
controllerContext.HttpContext.Request.InputStream.CopyTo(ms);
return ms.GetBuffer();
}
}
}
可以像这样注册:
[HttpPost]
public ActionResult Receive([ModelBinder(typeof(BTModelBinder))] byte[] audio)
{
...
}
如果使用 multipart/form-data
那么您可以使用标准 HttpPostedFileBase
动作参数为 此处显示.
不隶属于 StackOverflow