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); “在 Microsoft.Samples.Networking.BackgroundTransfer.exe 中发生了“System.ArgumentException”类型的异常,但未在用户代码中处理

WinRT 信息:'uri':仅支持 'http' 和 'https' 方案上传内容。”

4

3 回答 3

1

您的答案就在异常消息中。

引用文档

支持 FTP,但仅在执行下载操作时。

所以你不能使用BackgroundUploaderFTP。

于 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

Had the same issue, this worked for me! :)

于 2013-12-12T17:28:34.963 回答
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);
    }
}
于 2014-04-30T14:32:35.047 回答