【发布时间】: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