File upload BackgroundTransferRequest提供文件作为post参数

File upload BackgroundTransferRequest提供文件作为post参数,file-upload,windows-phone-8,background-transfer,File Upload,Windows Phone 8,Background Transfer,网站接受文件上传,其中文件数据应作为参数提供,如下所示:http://website.com?act=upload&file=[文件数据] 我如何使用Windows Phone 8中的BackgroundTransferRequest将文件从隔离存储上载到此网站 到目前为止,我遇到的所有教程都只使用了一些通用的上传URI,比如与BTS.UploadLocation=[URI to file];BackgroundTransferService.AddBTS;但在我的情况下,我需要以某种方式将文件

网站接受文件上传,其中文件数据应作为参数提供,如下所示:http://website.com?act=upload&file=[文件数据]

我如何使用Windows Phone 8中的BackgroundTransferRequest将文件从隔离存储上载到此网站

到目前为止,我遇到的所有教程都只使用了一些通用的上传URI,比如与BTS.UploadLocation=[URI to file];BackgroundTransferService.AddBTS;但在我的情况下,我需要以某种方式将文件数据与file POST参数绑定。

请使用以下代码:

只需调用Upload函数并传递参数,如xyz.png、文件字节[]、服务器URL。 其他的事情都是一样的。。 您将在响应函数中得到响应。 在我的情况下,我收到上传的文件url。 这对我有用。 我也希望你。 祝你好运

public void Upload(string name, byte[] content, String uriStr)
        {
            string boundary = Guid.NewGuid().ToString();
            string header = "--" + boundary;
            string footer = "--" + boundary + "--";

            HttpWebRequest uploadRequest = (HttpWebRequest)WebRequest.Create(uriStr);
            uploadRequest.ContentType = "multipart/form-data; boundary=" + boundary;
            uploadRequest.Method = "POST";

            StringBuilder headers = new StringBuilder();
            headers.AppendLine(header);
            headers.AppendLine("Content-Disposition: file; name=\"file\"; filename=\"" + name + "\"");
            headers.AppendLine("Content-Type: application/octet-stream");
            headers.AppendLine();
            headers.AppendLine(Encoding.GetEncoding("iso-8859-1").GetString(content, 0, content.Length));
            headers.AppendLine(footer);
            Console.Write(headers.ToString());

            byte[] contents = Encoding.GetEncoding("iso-8859-1").GetBytes(headers.ToString());
            object[] data = new object[2] { uploadRequest, contents };
            uploadRequest.BeginGetRequestStream(new AsyncCallback(GetData), data);
        }


public void GetData(IAsyncResult result)
        {
            object[] data = (object[])result.AsyncState;
            byte[] content = (byte[])data[1];

            HttpWebRequest request = (HttpWebRequest)data[0];
            Stream requestStream = request.EndGetRequestStream(result);
            int start = 0, count = 1024;

            while (true)
            {
                if (start + count > content.Length)
                {
                    requestStream.Write(content, start, (content.Length - start));
                    break;
                }
                requestStream.Write(content, start, count);
                start += count;
            }

            requestStream.Close();
            request.BeginGetResponse(new AsyncCallback(GetResponse), request);
        }

public void GetResponse(IAsyncResult result)
        {
            string imageUploadedUrl = "";

            try
            {
                HttpWebRequest request = (HttpWebRequest)result.AsyncState;
                HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(result);
                using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                {
                    string line;
                    while ((line = reader.ReadLine()) != null)
                    {
                        string check = line.Substring(0, 9);
                        if (check == "    <Url>")
                        {
                            imageUploadedUrl = line.Substring(9, (line.Length - 15));
                            break;
                        }
                    }
           }
     }
}

对不起,这没有道理。上载可以作为http POST完成,也可以作为带有url参数的http GET完成。你们不能两者兼得。@arkascha也许我弄错了,但这里有一个网站摘录,解释了上传过程:2。申请表对收到的URL发出请求。请求应包括包含图像JPG、PNG、BMP或GIF的文件的照片字段,该文件很可能为JPG、PNG、BMP或GIF格式。但是这些参数被编码为POST参数,这与您在问题中建议的GET参数不同。我建议您了解差异并编辑您的问题以获得答复。@arkascha OK。我使用GET style只是为了说明我的观点,我需要在我的BackgroundTransferRequest中使用photo参数。如果你能建议一个更好的例子,我很乐意把它包括在我的问题中。好吧,我不知道这里需要说明什么。。。你想用我不知道的BackgroundTransferRequest做一个POST请求。帖子应该包含两个参数:一个标量'act',值为'upload',一个文件参数'file',保存要上传的文件的内容。据我所知,这段代码没有使用BackgroundTransferRequest,而是使用HttpWebRequest。嗯,这是一个选择,如果它不能与基站完成。在应用程序关闭时上传文件的能力对我来说很重要。哦…很抱歉。但我已经实现了HttpWebRequest。