在 Windows 商店应用程序上上传文件时遇到一些问题。我得到了状态 200,但是当我检查服务器时没有上传文件..我使用的是 apache 服务器,我检查了 Microsoft后台传输示例的示例,但它适用于 ASP .Net 服务器。
任何解决方案?
我需要一次上传一个文件,它对我有用,请告诉我:
上传.php:
<?php
$target = "data/";
$target = $target . basename( $_FILES['Filename']['name']) ;
if(move_uploaded_file($_FILES['Filename']['tmp_name'], $target))
{
echo "The file ". basename( $_FILES['Filename']['name']). " has been uploaded";
}
else {
echo "Sorry, there was a problem uploading your file.";
}
?>
C#代码:
private async void StartMultipartUpload_Click(object sender, RoutedEventArgs e)
{
Uri uri;
if (!Uri.TryCreate(serverAddressField.Text.Trim(), UriKind.Absolute, out uri))
{
rootPage.NotifyUser("Invalid URI.", NotifyType.ErrorMessage);
return;
}
// Verify that we are currently not snapped, or that we can unsnap to open the picker.
if (ApplicationView.Value == ApplicationViewState.Snapped && !ApplicationView.TryUnsnap())
{
rootPage.NotifyUser("File picker cannot be opened in snapped mode. Please unsnap first.", NotifyType.ErrorMessage);
return;
}
FileOpenPicker picker = new FileOpenPicker();
picker.FileTypeFilter.Add("*");
IReadOnlyList<StorageFile> files = await picker.PickMultipleFilesAsync();
if (files.Count == 0)
{
rootPage.NotifyUser("No file selected.", NotifyType.ErrorMessage);
return;
}
List<BackgroundTransferContentPart> parts = new List<BackgroundTransferContentPart>();
for (int i = 0; i < files.Count; i++)
{
BackgroundTransferContentPart part = new BackgroundTransferContentPart("Filename", files[i].Name);
part.SetFile(files[i]);
parts.Add(part);
}
BackgroundUploader uploader = new BackgroundUploader();
UploadOperation upload = await uploader.CreateUploadAsync(uri, parts);
String fileNames = files[0].Name;
for (int i = 1; i < files.Count; i++)
{
fileNames += ", " + files[i].Name;
}
Log(String.Format("Uploading {0} to {1}, {2}", fileNames, uri.AbsoluteUri, upload.Guid));
// Attach progress and completion handlers.
await HandleUploadAsync(upload, true);
}
我使用了 SDK 示例的相同代码。data/文件夹位于upload.php的同一文件夹中,我的 Uri 是http://mySite/myApp/upload.php
好的,所以我找到了一个完整的解决方案。基本上你需要在你的 SDK 示例上作为多部分上传(不管它是 C# 还是 C++),关键是 php 文件。这是我处理所有选定文件的 PHP 文件。作为魅力!此外,如果您收到错误 1,则意味着您的 php.ini 中的最大文件上传大小太小!所以也要注意这一点。希望它有所帮助,我确实花了一些精力来完成这项工作。
<?php
$myFile = "testFile.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
foreach ($_FILES as $name) {
if ($_FILES[$name]["error"] > 0){
fwrite($fh, "Error: " . $_FILES[$name]['error'] . " on file: " . $_FILES[$name]['name']); // log the error
}
$target = "data/";
$target = $target . basename( $_FILES[$name]['name']) ;
if(move_uploaded_file($_FILES[$name]['tmp_name'], $target))
{
echo "The file ". basename( $_FILES[$name]['name']). " has been uploaded"; // succes, do whatever you want
}
else {
fwrite($fh, "File movement error on file: " . $_FILES[$name]['name']); // log the error
}
}
fclose($fh);
?>