通过POST multipart/formdata将文件从windows phone 8.1 Silverlight Httpclient上载到python服务器会损坏文件

2024-10-02 08:27:43 发布

您现在位置:Python中文网/ 问答频道 /正文

最初,我使用的是Windows Phone 8System.Net.Http库将图像从SD卡上载到Python服务器,代码如下:

    private async void UploadFile()
    {
        try
        {
            // Make sure there is a picture selected
            if (photoStream != null)
            {
                // initialize the client
                // need to make sure the server accepts network IP-based
                // requests. 
                // ensure correct IP and correct port address
                var fileUploadUrl = @"http://IPAddress/";
                var client = new HttpClient();

                // Reset the photoStream position
                // If you don't reset the position, the content lenght
                // sent will be 0
                photoStream.Position = 0;

                // This is the postdata
                MultipartFormDataContent content = new MultipartFormDataContent();
                content.Add(new StreamContent(photoStream), "fn", fileName);

                // upload the file sending the form info and ensure a result.
                // it will throw an exception if the service doesn't return 
                // a valid successful status code
                await client.PostAsync(fileUploadUrl, content)
                    .ContinueWith((postTask) =>
                    {
                        postTask.Result.EnsureSuccessStatusCode();
                    });
            }

            // Disable the Upload button
            btnUpload.IsEnabled = false;

            // reset the image control
            DisplayImage.Source = null;
        }
        catch
        {

        }
    }

在服务器端,我使用以下python代码对请求进行解码:

^{pr2}$

在我从WindowsPhone8切换到WindowsPhone8.1Silverlight之前,它一直有效。由于Windows Phone 8.1 Silverlight不再支持System.Net.Http库,因此我使用了Windows.Web.Http,并修改了代码:

    private async void UploadFile()
    {
        try
        {
            // Make sure there is a picture selected
            if (file != null)
            {
                // initialize the client
                // need to make sure the server accepts network IP-based
                // requests. 
                // ensure correct IP and correct port address
                var fileUploadUrl = new Uri(@"http://IPAddress/", UriKind.Absolute);
                var client = new HttpClient();

                HttpMultipartFormDataContent content = new HttpMultipartFormDataContent();
                HttpStreamContent streamContent = new HttpStreamContent(photoStream);
                content.Add(streamContent, "fn", fileName);

                await client.PostAsync(fileUploadUrl, content);

            }

            // Disable the Upload button
            btnUpload.IsEnabled = false;

            // reset the image control
            DisplayImage.Source = null;
        }
        catch
        {

        }
    }

服务器端保持不变。但是,上传的结果文件现在会定期插入额外的字节,看起来像'\r\n10000\r\n'或{}。文件似乎也以相同的额外字节模式开始和结束。在服务器上,pdict将边界映射到类似于xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx的内容,但这似乎与通过System.Net.Http上载文件时的行为相同。 我尝试在一个较小的文件中手动删除这些字节,该文件似乎可以确认该文件在其他方面是正确的。在

为什么会出现这些额外的字节?这是Python服务器错误处理POST请求的问题吗?在


Tags: 文件theipclienthttpnew字节var
1条回答
网友
1楼 · 发布于 2024-10-02 08:27:43

以下是我遇到这个问题时的解决方案:

客户:

 public async Task UploadImage(byte[] image, string url)
        {
            Stream stream = new System.IO.MemoryStream(image);
            HttpStreamContent streamContent = new HttpStreamContent(stream.AsInputStream());

            Uri resourceAddress = null;
            Uri.TryCreate(url.Trim(), UriKind.Absolute, out resourceAddress);
            Windows.Web.Http.HttpRequestMessage request = new Windows.Web.Http.HttpRequestMessage(Windows.Web.Http.HttpMethod.Post, resourceAddress);
            request.Content = streamContent;

            var httpClient = new Windows.Web.Http.HttpClient();
            var cts = new CancellationTokenSource();
            Windows.Web.Http.HttpResponseMessage response = await httpClient.SendRequestAsync(request).AsTask(cts.Token);
        }

控制器:

^{pr2}$

相关问题 更多 >

    热门问题