[英]How can I download a file from an FTP server with my Xamarinapp?
I'm new to Xamarin development.我是 Xamarin 开发的新手。 I hope that you can resolve my problem in a simple mode.
我希望您能以简单的方式解决我的问题。
I have a cross-platform app developed with Xamarin.我有一个使用 Xamarin 开发的跨平台应用程序。 In one page of the app I want to give the possibility to download a file (pdf document) when the user taps a button.
在应用程序的一个页面中,我想在用户点击按钮时提供下载文件(pdf 文档)的可能性。 How can I do it?
我该怎么做?
The file is hosted in an FTP server.该文件托管在 FTP 服务器中。
Please, if you can, give me a simple solution.如果可以的话,请给我一个简单的解决方案。 Thanks.
谢谢。
This is my button in page.xaml这是我在页面中的按钮。xaml
<StackLayout BackgroundColor="#2B2B2B" HeightRequest="60" Orientation="Horizontal" Padding="0" Margin="0" MinimumWidthRequest="-1">
<ImageButton VerticalOptions="Center" HorizontalOptions="Start" Source="pdf.png" HeightRequest="50" WidthRequest="50" Margin="10,0,0,0" ></ImageButton>
<Button VerticalOptions="Center" TextColor="White" BackgroundColor="Transparent" FontSize="20" FontAttributes="Italic" Text="Scarica il PDF"></Button>
</StackLayout>
The System.Net.WebClient
can be used cross platform for FTP calls. System.Net.WebClient
可以跨平台用于 FTP 调用。 Here you can find sample code: FtpCloudStorageClient.cs在这里您可以找到示例代码: FtpCloudStorageClient.cs
Have a look at the DownloadFileAsync()
function.看看
DownloadFileAsync()
function。 If you don't need SSL support, you can directly create a WebClient
instead of the derrived CustomFtpWebClient
.如果您不需要 SSL 支持,您可以直接创建
WebClient
而不是派生的CustomFtpWebClient
。 It would look something like this (untested):它看起来像这样(未经测试):
public override async Task<byte[]> DownloadFileAsync(string ftpUrl, string username, string password)
{
byte[] responseData;
using (WebClient webClient = new WebClient())
{
webClient.Credentials = new NetworkCredential(username, password);
responseData = await webClient.DownloadDataTaskAsync(ftpUrl);
}
return responseData;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.