只有“http”和“https”方案才支持上传内容

时间:2013-05-09 16:46:11

标签: file-upload windows-8 winrt-async

我正在尝试将文件上传到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'方案上传内容。“

3 个答案:

答案 0 :(得分:1)

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

引用the documentation

  

支持FTP,但仅限于进行下载操作。

所以你不能在FTP上使用BackgroundUploader

答案 1 :(得分: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

有同样的问题,这对我有用! :)

答案 2 :(得分: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);
    }
}