我需要我的Windows Phone应用程序能够使用MANGO中可用的BackgroundTransFerservice将音频文件上传到我的MVC3站点。

作为一种可能的解决方案,我可以:

  1. 将路线映射到我的控制器:

    public override void RegisterArea(AreaRegistrationContext context)
            {
                context.MapRoute(
                    "SingleAudioFile",
                    "Api/Audio/Recieve",
                    new { controller = "AudioFiles", action = "Recieve" }
                    );
            }
    
  2. 在控制器中,采取措施

    [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 动作参数为 此处显示.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top