2013-02-11 39 views

回答

1

我需要上傳一個單一的文件的時間,這對我的作品,讓我知道:

upload.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示例的相同代碼。 數據/文件夾處於upload.php的同一個文件夾,我的URI是http://mySite/myApp/upload.php

0

好了,所以我發現了一個完整的解決方案。基本上,您需要在您的SDK示例(無論它是否爲C#或C++)上以multipart的身份上傳,而關鍵是針對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); 

?>

相關問題