0

ファイルを ftp サーバーにアップロードしようとしています。次のコードを使用しています。

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); 「タイプ 'System.ArgumentException' の例外が Microsoft.Samples.Networking.BackgroundTransfer.exe で発生しましたが、ユーザー コードで処理されませんでした

WinRT 情報: 'uri': コンテンツのアップロードは、'http' および 'https' スキームでのみサポートされています。"

4

3 に答える 3

1

あなたの答えは例外メッセージにあります。

ドキュメントを引用するには:

FTPはサポートされていますが、ダウンロード操作を行う場合のみです。

BackgroundUploaderしたがって、FTPでは使用できません。

于 2013-05-09T16:58:45.277 に答える
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

同じ問題がありましたが、これでうまくいきました!:)

于 2013-12-12T17:28:34.963 に答える