silverlight ftp上传
我用silverlight实现了上传文件,但只是上传到本地
现在我想将本地的文件上传到ftp服务器上的一个文件夹中,
不知道怎么实现,
因为我对silverlight不熟,望高手能帮帮忙,给个简单的例子,谢谢
问题解决了我可以加分的
要点:
1.怎样连接ftp服务器
2.获取ftp服务器上文件夹的路径
3.将本地的文件上传到这个ftp服务器上的文件夹里
------解决方案--------------------
不会,先顶一个
------解决方案--------------------
C# code
ftp方式,这种方式是先吧file1的文件缓存到web服务器,再传到ftp服务器的,稍微修改,应该可以达到你的要求吧。
if (File1.PostedFile.FileName != "")
{
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://192.168.1.112:22/FtpTest/FtpReceive/" + Path.GetFileName(File1.PostedFile.FileName));
request.Credentials = new NetworkCredential("XXXX", "XXXX");
request.KeepAlive = true;
request.Method = WebRequestMethods.Ftp.UploadFile;
request.UseBinary = true;
Stream inputStream = File1.PostedFile.InputStream;
long len = inputStream.Length;
request.ContentLength = len;
Stream requestStream = request.GetRequestStream();
try
{
int count = 0;
byte[] buffer = new byte[2048];
while ((count = inputStream.Read(buffer, 0, buffer.Length)) > 0)
{
requestStream.Write(buffer, 0, count);
}
}
catch
{
}
finally
{
inputStream.Close();
inputStream.Dispose();
requestStream.Close();
requestStream.Dispose();
}
}
------解决方案--------------------
使用webclient试试,C# code
/// <summary>
/// WebClient上传文件至服务器
/// </summary>
/// <param name="Filepath">文件路径</param>
/// <returns></returns>
public void UploadFile(string Filepath)
{
try
{
//获取文件名
string File = Filepath.Substring(Filepath.LastIndexOf("/") + 1).ToString();
//设置要上传文件的路径
string localFilePath = Localpath + "/" + File;
//设置ftp路径
string serverFolder = Serverpath + "/" + File;
//创建WebClient实例
WebClient myWebClient = new WebClient();
myWebClient.Credentials = CredentialCache.DefaultCredentials;
//myWebClient.UploadFile(serverFolder, "PUT", localFilePath);
FileStream fs = new FileStream(localFilePath, FileMode.Open, FileAccess.Read);
BinaryReader r = new BinaryReader(fs);
byte[] postArray = r.ReadBytes((int)fs.Length);
Stream postStream = myWebClient.OpenWrite(serverFolder, "PUT");
if (postStream.CanWrite)
{
postStream.Write(postArray, 0, postArray.Length);
}
postStream.Close();
}
catch (Exception ex)
{
Lost += "上传文件名为:" + Filepath + "时失败;错误原因:"+ex.Message+"\r\n";
}
}
------解决方案--------------------
Silverlight作为客户端,不需要关心该目录是属于FTP或者HTTP,只是通过网络上传文件到指定目录即可,而FTP服务器可以映射上传目录为FTP服务器目录之一就可以了。
至于Silverlight上传技术有很多,下面有不少开源项目可以下载参考学习,如果在开发中遇到问题,可以提问一起讨论:
Silverlight支持Ftp上传
http://silverlightchina.net/html/tips/2010/1103/3142.html
http://silverlightchina.net/html/tips/2010/1201/3791.html
http://silverlightchina.net/html/tips/2010/1230/4543.html
silverlight ftp上传我用silverlight实现了上传文件,但只是上传到本地现在我想将本地的文件上传到ftp服务器上的一个文件夹中,不知道怎么实现,因为我对silverlight不熟,望高手能帮帮忙,给个简单的例子,谢谢问题解决了我可以加分的要点:1.怎样连接ftp服务器2.获取ftp服务器上文件夹的路径3.将本地的文件上传到这个ftp服务器上的文件夹里------解决方案--------------------不会,先顶一个 ------解决方案--------------------C# codeftp方式,这种方式是先吧file1的文件缓存到web服务器,再传到ftp服务器的,稍微修改,应该可以达到你的要求吧。 if (File1.PostedFile.FileName != "") { FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://192.168.1.112:22/FtpTest/FtpReceive/" + Path.GetFileName(File1.PostedFile.FileName)); request.Credentials = new NetworkCredential("XXXX", "XXXX"); request.KeepAlive = true; request.Method = WebRequestMethods.Ftp.UploadFile; request.UseBinary = true; Stream inputStream = File1.PostedFile.InputStream; long len = inputStream.Length; request.ContentLength = len; Stream requestStream = request.GetRequestStream(); try { int count = 0; byte[] buffer = new byte[2048]; while ((count = inputStream.Read(buffer, 0, buffer.Length)) > 0) { requestStream.Write(buffer, 0, count); } } catch { } finally { inputStream.Close(); inputStream.Dispose(); requestStream.Close(); requestStream.Dispose(); } }------解决方案--------------------使用webclient试试,C# code/// /// WebClient上传文件至服务器 /// /// 文件路径 /// public void UploadFile(string Filepath) { try { //获取文件名 string File = Filepath.Substring(Filepath.LastIndexOf("/") + 1).ToString(); //设置要上传文件的路径 string localFilePath = Localpath + "/" + File; //设置ftp路径 string serverFolder = Serverpath + "/" + File; //创建WebClient实例 WebClient myWebClient = new WebClient(); myWebClient.Credentials = CredentialCache.DefaultCredentials; //myWebClient.UploadFile(serverFolder, "PUT", localFilePath); FileStream fs = new FileStream(localFilePath, FileMode.Open, FileAccess.Read); BinaryReader r = new BinaryReader(fs); byte[] postArray = r.ReadBytes((int)fs.Length); Stream postStream = myWebClient.OpenWrite(serverFolder, "PUT"); if (postStream.CanWrite) { postStream.Write(postArray, 0, postArray.Length); } postStream.Close(); } catch (Exception ex) { Lost += "上传文件名为:" + Filepath + "时失败;错误原因:"+ex.Message+"\r\n"; } }------解决方案--------------------Silverlight作为客户端,不需要关心该目录是属于FTP或者HTTP,只是通过网络上传文件到指定目录即可,而FTP服务器可以映射上传目录为FTP服务器目录之一就可以了。至于Silverlight上传技术有很多,下面有不少开源项目可以下载参考学习,如果在开发中遇到问题,可以提问一起讨论:Silverlight支持Ftp上传http://silverlightchina.net/html/tips/2010/1103/3142.htmlhttp://silverlightchina.net/html/tips/2010/1201/3791.htmlhttp://silverlightchina.net/html/tips/2010/1230/4543.html
http://silverlightchina.net/html/tips/2011/0218/5448.html
http://silverlightchina.net/html/works/2011/0321/6235.html
http://silverlightchina.net/html/works/2011/0324/6331.html
http://silverlightchina.net/html/tips/2011/0512/7581.html
------解决方案--------------------
楼上不错。支持下