UWP Fails to POST Files

Asked 2 years ago, Updated 2 years ago, 76 views

I want to post-send the file from the UWP application to the PHP application on the server side, but the file fails to be sent.
The failure is that the $_FILES decision on the PHP side is always false.

I have sent two parameters.
- param1 is a string value
- param2 is the upload file

The server side (sample.php) makes the following decisions

 if ($_FILES['param2'])

The code on the UWP side that currently fails is as follows

private async Task PostSample()
{
    const string POST_URL = "http://xxx.xxx.xxx.xxx/api/sample.php";

    var multi=new HttpMultipartFormDataContent();

    varparam1 = new HttpStringContent("foo");
    multi.Add (param1, "param1");

    byte [ ] values = new byte [ ] { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 };
    varparam2 = new HttpStreamContent(new MemoryStream(values).AsInputStream());
    param2.Headers.ContentDisposition=newWindows.Web.Http.Headers.HttpContentDispositionHeaderValue("form-data")
    {
        Name = "param2",
        FileName="piyo.dat"
    };
    multi.Add (param2);

    varclient = new HttpClient();
    var res = wait client.PostAsync (new Uri(POST_URL), multi);

    vardiag = new MessageDialog(res.ToString());
    wait diag.ShowAsync();
}

External Tools

The Google Chrome app Advanced REST Client succeeded (if($_FILES['param2']) is true.

Executing similar code in Windows Forms succeeded

private async Task PostSample()
{
    const string POST_URL = "http://xxx.xxx.xxx.xxx/api/sample.php";

    var multi=new MultipartFormDataContent();

    var param1 = new StringContent("foo");
    multi.Add (param1, "param1");

    byte [ ] values = new byte [ ] { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 };
    varparam2 = new StreamContent (new MemoryStream (values));
    param2.Headers.ContentDisposition=newSystem.Net.Http.Headers.ContentDispositionHeaderValue("form-data")
    {
        Name = "param2",
        FileName="piyo.dat"
    };
    multi.Add (param2);

    varclient = new HttpClient();
    var res = wait client.PostAsync(POST_URL, multi);
    MessageBox.Show(res.ToString());
}

Therefore,
I suspect that the code on the UWP side is incorrect, missing, or some specification on the UWP side, but I don't know why.
I would appreciate it if you could give me some advice on the direction of the investigation.

environment

  • Client
    • Windows 10 (14393) 64bit, .NET Framework 4.6.1
  • servers
    • Windows Server 2008+IIS 7.0
  • Windows 10 (14393) 64bit, .NET Framework 4.6.1
  • Windows Server 2008+IIS 7.0

It may be inappropriate, but I also posted it on the English version of Stackoverflow yesterday.However, it seems that the view doesn't work in the first place, so I also posted it here.
If it is not appropriate, I will delete it, so I would appreciate it if you could let me know.

https://stackoverflow.com/questions/40988582/post-transmission-of-file-fails-with-uwp

php c# .net http win-universal-app

2022-09-30 14:41

1 Answers

If WinForm works with almost the same code, it is possible that the app does not have permission to send data to the outside world.

Declare app features


2022-09-30 14:41

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.