【问题标题】:Upload image to a FTP Server using PCL Xamarin Forms使用 PCL Xamarin Forms 将图像上传到 FTP 服务器
【发布时间】:2017-07-26 20:22:21
【问题描述】:

我是 Xamarin 和 C# 世界的新手,我正在尝试将图像上传到 FTP 服务器。我看到了 FtpWebRequest 类来执行此操作,但我没有做对,我不知道如何注入特定于平台的代码,我什至不知道它的真正含义,已经看过这个视频 (https://www.youtube.com/watch?feature=player_embedded&v=yduxdUCKU1c) 但是我不知道如何使用它来创建 FtpWebRequest 类并上传图像。

我看到此代码(此处:https://forums.xamarin.com/discussion/9052/strange-behaviour-with-ftp-upload)发送图片,但我无法使用它。

public void sendAPicture(string picture)
{

    string ftpHost = "xxxx";

    string ftpUser = "yyyy";

    string ftpPassword = "zzzzz";

    string ftpfullpath = "ftp://myserver.com/testme123.jpg";

    FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpfullpath);

    //userid and password for the ftp server  

    ftp.Credentials = new NetworkCredential(ftpUser, ftpPassword);

    ftp.KeepAlive = true;
    ftp.UseBinary = true;
    ftp.Method = WebRequestMethods.Ftp.UploadFile;

    FileStream fs = File.OpenRead(picture);

    byte[] buffer = new byte[fs.Length];
    fs.Read(buffer, 0, buffer.Length);

    fs.Close();

    Stream ftpstream = ftp.GetRequestStream();
    ftpstream.Write(buffer, 0, buffer.Length);
    ftpstream.Close();
    ftpstream.Flush();

    //  fs.Flush();

}

我没有 FileStream、WebRequestMethods 和 File 类型,我的 FtpWebRequest 类也没有“KeepAlive”、“UseBinary”和“GetRequestStream”方法,我的 Stream 类没有“Close”方法。

我的 FtpWebRequest 类:

公共密封类 FtpWebRequest : WebRequest { 公共覆盖字符串 ContentType { 得到 { 抛出新的 NotImplementedException(); }

    set
    {
        throw new NotImplementedException();
    }
}

public override WebHeaderCollection Headers
{
    get
    {
        throw new NotImplementedException();
    }

    set
    {
        throw new NotImplementedException();
    }
}

public override string Method
{
    get
    {
        throw new NotImplementedException();
    }

    set
    {
        throw new NotImplementedException();
    }
}

public override Uri RequestUri
{
    get
    {
        throw new NotImplementedException();
    }
}

public override void Abort()
{
    throw new NotImplementedException();
}

public override IAsyncResult BeginGetRequestStream(AsyncCallback callback, object state)
{
    throw new NotImplementedException();
}

public override IAsyncResult BeginGetResponse(AsyncCallback callback, object state)
{
    throw new NotImplementedException();
}

public override Stream EndGetRequestStream(IAsyncResult asyncResult)
{
    throw new NotImplementedException();
}

public override WebResponse EndGetResponse(IAsyncResult asyncResult)
{
    throw new NotImplementedException();
}

}

(我知道,我没有在那里写任何东西,只是按了 ctrl + 。因为我不知道在那里写什么)

有人可以提供 FtpWebRequest 类的完整示例吗?我只找到像上面这样使用的类。

【问题讨论】:

    标签: c# ftp xamarin.forms image-uploading ftpwebrequest


    【解决方案1】:

    好的,我刚刚想出了如何做到这一点,我将展示我是如何做到的,我真的不知道是否是更好和正确的方法,但它确实有效。

    首先我必须在我的表单项目上创建一个名为 IFtpWebRequest 的接口类,其中包含以下内容:

    namespace Contato_Vistoria
    {
            public interface IFtpWebRequest
            {
                string upload(string FtpUrl, string fileName, string userName, string password, string UploadDirectory = "");
            }
    }
    

    然后,在我的 iOS/droid 项目中,我必须创建一个名为 FTP 的类来实现 IFtpWebRequest,并且在该类中我编写了上传函数(我现在正在使用另一个),这是整个 FTP 类:

    using System;
    using System.IO;
    using System.Net;
    using Contato_Vistoria.Droid; //My droid project
    
    [assembly: Xamarin.Forms.Dependency(typeof(FTP))] //You need to put this on iOS/droid class or uwp/etc if you wrote
    namespace Contato_Vistoria.Droid
    {
        class FTP : IFtpWebRequest
        {
            public FTP() //I saw on Xamarin documentation that it's important to NOT pass any parameter on that constructor
            {
            }
    
            /// Upload File to Specified FTP Url with username and password and Upload Directory if need to upload in sub folders
            ///Base FtpUrl of FTP Server
            ///Local Filename to Upload
            ///Username of FTP Server
            ///Password of FTP Server
            ///[Optional]Specify sub Folder if any
            /// Status String from Server
            public string upload(string FtpUrl, string fileName, string userName, string password, string UploadDirectory = "")
            {
                try
                {
    
                    string PureFileName = new FileInfo(fileName).Name;
                    String uploadUrl = String.Format("{0}{1}/{2}", FtpUrl, UploadDirectory, PureFileName);
                    FtpWebRequest req = (FtpWebRequest)FtpWebRequest.Create(uploadUrl);
                    req.Proxy = null;
                    req.Method = WebRequestMethods.Ftp.UploadFile;
                    req.Credentials = new NetworkCredential(userName, password);
                    req.UseBinary = true;
                    req.UsePassive = true;
                    byte[] data = File.ReadAllBytes(fileName);
                    req.ContentLength = data.Length;
                    Stream stream = req.GetRequestStream();
                    stream.Write(data, 0, data.Length);
                    stream.Close();
                    FtpWebResponse res = (FtpWebResponse)req.GetResponse();
                    return res.StatusDescription;
    
                }
                catch(Exception err)
                {
                    return err.ToString();
                }
            }
        }
    }
    

    这在我的 iOS 项目中几乎相同,但我还是会发布它以帮助像我这样不太了解并需要查看完整示例的人。这里是:

    using System;
    using System.Net;
    using System.IO;
    //Only thing that changes to droid class is that \/
    using Foundation;
    using UIKit;
    using Contato_Vistoria.iOS;
    
    
    [assembly: Xamarin.Forms.Dependency(typeof(FTP))]
    namespace Contato_Vistoria.iOS  //   /\
    {
        class FTP : IFtpWebRequest
        {
            public FTP()
            {
    
            }
    
            /// Upload File to Specified FTP Url with username and password and Upload Directory if need to upload in sub folders
            ///Base FtpUrl of FTP Server
            ///Local Filename to Upload
            ///Username of FTP Server
            ///Password of FTP Server
            ///[Optional]Specify sub Folder if any
            /// Status String from Server
            public string upload(string FtpUrl, string fileName, string userName, string password, string UploadDirectory = "")
            {
                try
                {
                    string PureFileName = new FileInfo(fileName).Name;
                    String uploadUrl = String.Format("{0}{1}/{2}", FtpUrl, UploadDirectory, PureFileName);
                    FtpWebRequest req = (FtpWebRequest)FtpWebRequest.Create(uploadUrl);
                    req.Proxy = null;
                    req.Method = WebRequestMethods.Ftp.UploadFile;
                    req.Credentials = new NetworkCredential(userName, password);
                    req.UseBinary = true;
                    req.UsePassive = true;
                    byte[] data = File.ReadAllBytes(fileName);
                    req.ContentLength = data.Length;
                    Stream stream = req.GetRequestStream();
                    stream.Write(data, 0, data.Length);
                    stream.Close();
                    FtpWebResponse res = (FtpWebResponse)req.GetResponse();
                    return res.StatusDescription;
    
                }
                catch (Exception err)
                {
                    return err.ToString();
                }
            }
        }
    }
    

    最后,回到我的 Xamarin Forms 项目,这就是我调用函数的方式。在我的 GUI 上的按钮的简单点击事件中:

        protected async void btConcluidoClicked(object sender, EventArgs e)
        {
            if (Device.OS == TargetPlatform.Android || Device.OS == TargetPlatform.iOS)
                await DisplayAlert("Upload", DependencyService.Get<IFtpWebRequest>().upload("ftp://ftp.swfwmd.state.fl.us", ((ListCarImagesViewModel)BindingContext).Items[0].Image, "Anonymous", "gabriel@icloud.com", "/pub/incoming"), "Ok");
    
            await Navigation.PopAsync();
        }
    

    调用函数需要写“DependencyService.Get().YourFunction(Parameters of the function)”,更具体一点。

    我就是这样做的,希望能帮到别人。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-27
      • 2017-05-31
      • 2019-09-14
      • 2017-06-11
      • 1970-01-01
      • 2017-06-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多