BackgroundTransferRequest提供文件作为post参数

时间:2013-10-23 07:49:21

标签: file-upload windows-phone-8 background-transfer

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

如何在Windows Phone 8中使用BackgroundTransferRequest将文件从IsolatedStorage上传到此网站?

到目前为止,我遇到的所有教程只是使用了一些通用的上传URI(类似http://website.com/upload/而没有参数)和BTS.UploadLocation = [Uri to file]; BackgroundTransferService.Add(BTS);但在我的情况下,我需要以某种方式将文件数据绑定到{ {1}} POST参数。

1 个答案:

答案 0 :(得分:0)

请使用以下代码:

只需调用上传功能并传递“xyz.png”,文件字节[],服务器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;
                        }
                    }
           }
     }
}