我在Windows 8.1商店应用程序上使用Windows.Web.Http测试(将在Windows Phone 8.1上执行此问题)。
这就是我所做的:
HttpMultipartFormDataContent formData = new HttpMultipartFormDataContent();
IStorageFolder aFolder = await StorageFolder.GetFolderFromPathAsync(imageFolder);
IStorageFile aFile = await aFolder.GetFileAsync("myImage.jpg");
IInputStream aStream = await aFile.OpenAsync(FileAccessMode.Read);
formData.Add(new HttpStreamContent(aStream), "UploadFile", aFile.Name); // This is the problem. Remove this and HttpStreamContent captured well.
formData.Add(new HttpStringContent("some text"), "somename");
formData.Add(new HttpStringContent("another text"), "anothername");
HttpClient aClient = new HttpClient();
aClient.DefaultRequestHeaders.UserAgent.Clear();
aclient.DefaultRequestHeaders.UserAgent.ParseAdd("Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.130 Safari/537.36");
aClient.DefaultRequestHeaders.Accept.ParseAdd("text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
aClient.DefaultRequestHeaders.AcceptLanguage.ParseAdd("en-US,en;q=0.8,ms;q=0.6,id;q=0.4");
aClient.DefaultRequestHeaders.AcceptEncoding.ParseAdd("gzip, deflate, sdch");
HttpResponseMessage aResponse = new HttpResponseMessage();
aResponse = await aClient.PostAsync(aURI, formData);
aResponse.EnsureSuccessStatusCode();
aResponseHTML = await aResponse.Content.ReadAsStringAsync();
主机回复错误500内部服务器错误。 如果我删除文件上传,没有错误。使用主机的PHP脚本记录的数据很好。 我相信主机的PHP脚本部分工作正常,相同的脚本适用于HTML webform和使用MS Visual Foxpro开发的另一个应用程序。
并尝试发送文件,
IStorageFolder aFolder = await StorageFolder.GetFolderFromPathAsync(imageFolder);
IStorageFile aFile = await aFolder.GetFileAsync("myImage.jpg");
IInputStream aStream = await aFile.OpenAsync(FileAccessMode.Read);
IHttpContent aContent = new HttpStreamContent(aStream);
aContent.Headers.ContentType = new HttpMediaTypeHeaderValue("image/png");
aContent.Headers.ContentDisposition = new HttpContentDispositionHeaderValue("form-data")
{
Name = "UploadFile",
FileName = string.Format("\"{0}\"", aFile.Name) // found in a blog post that the name should be in quotes, else again error 500
};
HttpRequestMessage aRequest = new HttpRequestMessage(HttpMethod.Post, aURI);
aRequest.Content = aContent;
HttpClient aClient = new HttpClient();
aClient.DefaultRequestHeaders.UserAgent.Clear();
aclient.DefaultRequestHeaders.UserAgent.ParseAdd("Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.130 Safari/537.36");
aClient.DefaultRequestHeaders.Accept.ParseAdd("text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
aClient.DefaultRequestHeaders.AcceptLanguage.ParseAdd("en-US,en;q=0.8,ms;q=0.6,id;q=0.4");
aClient.DefaultRequestHeaders.AcceptEncoding.ParseAdd("gzip, deflate, sdch");
aResponse = await aClient.SendRequestAsync(request);
这一次,好了。 在主机上失败。 PHP $ FILES数组不包含任何内容。 用于检测上传文件的PHP脚本:
if (isset($_FILES["UploadFile"]["name"])) { ....
答案 0 :(得分:0)
在您第一次请求时,您发送的Content-Type: multipart/form-data
请求由包含标题和正文的较小部分组成。见here for more information。这应该适用于$_FILES
。
您可以使用以下PHP脚本来验证收到的数据:
<?php
print_r($_FILES);
?>
我使用的是PHP 5.6.9,它运行正常。
在您的第二个请求中,您发送的是Content-Type: image/png
,您的文件是整个请求正文,$_FILES
在这种情况下无效,您需要阅读所有请求:< / p>
<?php
$myFile = @file_get_contents('php://input');
?>