2013-05-09 4 views
0

내가 FTP 서버에 파일을 업로드하기 위해 노력하고있어, 나는이 코드를 사용하고 있습니다 지원됩니다 :업로드 콘텐츠는 'http'와 'https'제도가

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("*"); 
     StorageFile file = await picker.PickSingleFileAsync(); 

     if (file == null) 
     { 
      rootPage.NotifyUser("No file selected.", NotifyType.ErrorMessage); 
      return; 
     } 
     PasswordCredential pw = new PasswordCredential(); 
     pw.Password = "pass"; 
     pw.UserName = "username"; 
     BackgroundUploader uploader = new BackgroundUploader(); 
     uploader.ServerCredential = pw; 
     uploader.Method = "POST"; 
     uploader.SetRequestHeader("Filename", file.Name); 

     UploadOperation upload = uploader.CreateUpload(uri, file); 
     Log(String.Format("Uploading {0} to {1}, {2}", file.Name, uri.AbsoluteUri, upload.Guid)); 

     // Attach progress and completion handlers. 
     await HandleUploadAsync(upload, true); 

를하지만 나에게이 예외를 전송 여기 : UploadOperation upload = uploader.CreateUpload (uri, file); "Microsoft.Samples.Networking.BackgroundTransfer.exe에서 'System.ArgumentException'유형의 예외가 발생했지만 사용자 코드에서 처리되지 않았습니다.

WinRT 정보 : 'uri': 콘텐츠 업로드는 'http'및 'https'계획. "

답변

1

귀하의 대답은 예외 메시지에 있습니다.

the documentation을 인용 :

FTP 지원, 만 다운로드 작업을 수행 할 때입니다.

따라서 FTP에는 BackgroundUploader을 사용할 수 없습니다.

+0

그래서 무엇을해야 나는한다? –

+1

응용 프로그램이 실행되는 동안 일반 FTP 업로드를 수행하거나 업로드를 허용하고 FTP 서버에 푸시하는 HTTP 서버를 작성할 수 있습니다. 백그라운드 FTP 업로드를 할 방법이 없습니다. –

+0

안녕하세요 스테판, 회신 해 주셔서 감사합니다!이 목적에 대해 정말 아무것도 찾을 수 없기 때문에 당신이 내게 예제 또는 tuto 줄 수 있습니까? 감사합니다 :) –

0
Public Async Function FTP_Uploader(ftpURL As String, filename As String, username As String, password As String, file as StorageFile) As Task(Of Boolean) 
     Try 
      Dim request As WebRequest = WebRequest.Create(ftpURL + "/" + filename) 
      request.Credentials = New System.Net.NetworkCredential(username.Trim(), password.Trim()) 
      request.Method = "STOR" 
      Dim buffer As Byte() = ReadFiletoBinary(filename, file) 
      Dim requestStream As Stream = Await request.GetRequestStreamAsync() 
      Await requestStream.WriteAsync(buffer, 0, buffer.Length) 
      Await requestStream.FlushAsync() 
      Return True 
     Catch ex As Exception 
      Return False 
     End Try 
End Function 

Public Shared Async Function ReadFileToBinary(ByVal filename As String, file As StorageFile) As Task(Of Byte()) 

     Dim readStream As IRandomAccessStream = Await file.OpenAsync(FileAccessMode.Read) 
     Dim inputStream As IInputStream = readStream.GetInputStreamAt(0) 

     Dim dataReader As DataReader = New DataReader(inputStream) 
     Dim numBytesLoaded As UInt64 = Await dataReader.LoadAsync(Convert.ToUInt64(readStream.Size)) 

     Dim i As UInt64 
     Dim b As Byte 
     Dim returnvalue(numBytesLoaded) As Byte 

     While i < numBytesLoaded 
      inputStream = readStream.GetInputStreamAt(i) 
      b = dataReader.ReadByte() 
      returnvalue(i) = b 
      i = i + 1 
     End While 

     readStream.Dispose() 
     inputStream.Dispose() 
     dataReader.Dispose() 
     Return returnvalue 

End Function 

동일한 문제가 발생하면이 문제가 발생했습니다. :)

0

동일한 문제가 발생했습니다. 하루 일과를 마치고 WebRequest 클래스에서 작업 할 수있게되었습니다. 다운로드 기능을

완전히 작동 응용 프로그램은 여기에 있습니다 : http://code.msdn.microsoft.com/windowsapps/CSWindowsStoreAppFTPDownloa-88a90bd9

내가 너무 서버에 업로드를 사용하려면이 코드를 수정했습니다.

는 파일 업로드를위한 것입니다

public async Task UploadFTPFileAsync(Uri destination, StorageFile targetFile) 
{ 
    var request = WebRequest.Create(destination); 
    request.Credentials = Credentials; 
    request.Method = "STOR"; 

    using (var requestStream = (await request.GetRequestStreamAsync())) 
    using (var stream = await targetFile.OpenStreamForReadAsync()) 
    { 
     stream.CopyTo(requestStream); 
    } 
} 

을 그리고 이것은 만드는 디렉토리입니다 :

public async Task CreateFTPDirectoryAsync(Uri directory) 
{ 
    var request = WebRequest.Create(directory); 
    request.Credentials = Credentials; 
    request.Method = "MKD"; 

    using (var response = (await request.GetResponseAsync())) 
    { 
     //flush 
     //using will call the (hidden!) close method, which will finish the request. 
    } 
} 

request.Credentials이 같은 NetworkCredential 가득 할 수 있습니다

private string strFtpAccount; 
private string strFtpPassword; 
private string strFtpDomain; 

public ICredentials Credentials 
{ 
    get 
    { 
     return new NetworkCredential(strFtpAccount, strFtpPassword, strFtpDomain); 
    } 
}