BackgroundTransferRequest WP7 [英] BackgroundTransferRequest WP7

查看:104
本文介绍了BackgroundTransferRequest WP7的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Background Transfer将照片上传到我的Web服务.由于照片上传会占用大量时间和内存,因此我认为使用后台传输请求来完成此操作可能是一个不错的主意.上传照片后,我想获取上传照片的ID,然后将其用于后处理.但是,事实证明我无法在后台传输请求中做到这一点.

I am using the Background Transfer to upload Photographs to my Web Service. As the Photograph uploads can consume significant time and memory, I thought it might be a nice idea to use the background transfer request to accomplish this. After the photo is uploaded, I want to obtain the Id of the uploaded photo and then use it for post-processing. However, it turns out I can't do that in a background transfer request.

据我了解,后台传输仅使用以下逻辑进行工作:

Per my understanding, Background Transfer works using the following logic ONLY:

  1. 您必须获取要上传的文件,然后将其保存/复制到应用程序的独立存储中,该文件夹位于共享/传输文件夹下.这是非常重要的.显然,在其他位置使用文件对我不起作用.也许这不是共享/转移,而是相对"路径.但我会坚持相同的约定.

  1. You have to obtain the file you want to upload and then save/copy it to your app's Isolated Storage under the folder: shared/transfers. This is extremely important. Apparently, using file in a different location didn't work for me. Maybe it isn't the shared/transfers as much as it is a 'relative' path. But I would stick to the same conventions.

在该位置保存文件后,可以基于该位置创建后台请求.看起来除了文件内容之外,您还不能传递POST CONTENT,因此,任何其他参数(例如文件名,mime类型等)仅需要作为QUERY String参数传递.我可以理解这一点,但是如果我可以将两者都作为POST内容传递,那就太好了.我认为HTTP对此没有任何限制.

After you have saved the file in that location, your background request can be created based on that. It doesn't look like you can pass POST CONTENT other than the file contents, so any other parameters like file name, mime type etc. will need to be passed as QUERY String parameters only. I can understand this, but it would've been nice if I could pass both as POST Content. I don't think HTTP has a limitation on how this works.

以下是一些使用Hammock创建请求的代码:

Here is some code for creating a request using Hammock:

string url = App.ZineServiceAuthority + "articles/save-blob?ContainerName={0}&MimeType={1}&ZineId={2}&Notes={3}&IsPrivate={4}&FileName={5}";
url = String.Format(url, userId, "image/jpg", ZineId, txtStatus.Text, true, UploadFileName);

var btr = new BackgroundTransferRequest(new Uri(url, UriKind.Absolute));
btr.TransferPreferences = TransferPreferences.AllowCellularAndBattery;
btr.Method = "POST";
btr.Headers.Add("token", IsolatedStorageHelper.GetTravzineToken());
btr.UploadLocation = new Uri(@"/shared\transfers/" + UploadFileName, UriKind.Relative);
btr.TransferStatusChanged += new EventHandler<BackgroundTransferEventArgs>(btr_TransferStatusChanged);
btr.TransferProgressChanged += new EventHandler<BackgroundTransferEventArgs>(btr_TransferProgressChanged);

BackgroundTransferService.Add(btr);

就我而言,我实际上是使用查询字符串传递所有必要的参数.成功保存后,我的Web服务将返回我刚刚上传的照片的ID.然而: 无法(或至少我不知道)获得和评估响应.后台传输请求事件处理程序不公开响应. 这是我的事件处理程序:

In my case, I am literally passing all the necessary parameters using the query string. On a successful save, my Web Service returns back the Id of the Photo I just uploaded. However: There is NO way (or at least I know of) to obtain and evaluate the RESPONSE. The Background Transfer Request Event handlers do not expose a RESPONSE. Here are my event handlers:

void btr_TransferProgressChanged(object sender, BackgroundTransferEventArgs e)
{
    bool isUploading = e.Request.TotalBytesToSend > 0 ? true : false;
    lblStatus.Text = isUploading ? "Uploading" + e.Request.BytesSent.ToString() + " sent" : "Done";
}

void btr_TransferStatusChanged(object sender, BackgroundTransferEventArgs e)
{
    if (e.Request.TransferStatus == TransferStatus.Completed)
    {

        using (IsolatedStorageFile iso =
               IsolatedStorageFile.GetUserStoreForApplication())
        {
            if (iso.FileExists(e.Request.UploadLocation.OriginalString))
                iso.DeleteFile(e.Request.UploadLocation.OriginalString);
        }

        BackgroundTransferService.Remove(e.Request);

        if (null != e.Request.TransferError)
        {
            MessageBox.Show(e.Request.TransferError.Message);
        }
        else
        {
            lblStatus.Text = "Done baby done";
        }

    }
}

所以现在我的问题是,在这种情况下,任何人如何进行任何类型的POST处理? 谁能告诉我设计这样一个僵化的课程背后的思路吗? 关于如何解决此问题的任何想法将不胜感激.

So now my question is, how does anyone do any sort of POST Processing in such scenarios? Can anyone please tell me the line of thought behind designing such an inflexible class? Any thoughts on how I could get around this issue would be appreciated.

还有,有人有本土化的BackgroundTransfer的工作示例吗?

Also, does anyone have any working examples of a homegrown BackgroundTransfer?

推荐答案

还没有尝试过,但是为什么不这样设置下载位置:

Haven't tried it but why not set a download location like this:

btr.DownloadLocation = "myDownloadFile.html";
btr.UploadLocation = "myUploadFile.jpg";
...

如果请求完成,请读取存储了您的响应的文件"myDownloadFile.html",然后将其删除.

If the request is completed read the file "myDownloadFile.html" where your response has been stored and delete it afterwards.

这篇关于BackgroundTransferRequest WP7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆