日期:2014-05-20  浏览次数:20368 次

如何用Asp.net中的File.Copy将文件保存到远程机器上.
就好像用CuteFTP上传文件一样,我想使用File.Copy(要复制的文件,目标文件)方法.
但这里有一个问题,假设目标文件为ftp://www.test.com/test.xml
我也具有对ftp://www.test.com写权限的用户名和密码.
如何将访问ftp:的用户名和密码写进代码中,使之将文件保存到远程机器上?
如何实现?

------解决方案--------------------
FileUpload 控件直接可以上传


------解决方案--------------------
要通过服务器提供的页面程序实现
file.saveas 不然没有权限的
------解决方案--------------------
webservice推送。。
------解决方案--------------------
www.codeproject.com上有ftpclient有你需要的功能
FtpClient ftp=new FtpClient( "ip ", "username ", "psw ");
ftp.Login();
string[] lists=ftp.GetFileList();
------解决方案--------------------
File.Copy根本不能写远程机器。除非你的远程是在指本地映射磁盘驱动器的“远程机器”,但是那个代码跟本地没有任何区别。
------解决方案--------------------
下面的代码示例演示如何使用异步操作将文件上载到 FTP 服务器。

C# 复制代码
using System;
using System.Net;
using System.Threading;

using System.IO;
namespace Examples.System.Net
{
public class FtpState
{
private ManualResetEvent wait;
private FtpWebRequest request;
private string fileName;
private Exception operationException = null;
string status;

public FtpState()
{
wait = new ManualResetEvent(false);
}

public ManualResetEvent OperationComplete
{
get {return wait;}
}

public FtpWebRequest Request
{
get {return request;}
set {request = value;}
}

public string FileName
{
get {return fileName;}
set {fileName = value;}
}
public Exception OperationException
{
get {return operationException;}
set {operationException = value;}
}
public string StatusDescription
{
get {return status;}
set {status = value;}
}
}
public class AsynchronousFtpUpLoader
{
// Command line arguments are two strings:
// 1. The url that is the name of the file being uploaded to the server.
// 2. The name of the file on the local machine.
//
public static void Main(string[] args)
{
// Create a Uri instance with the specified URI string.
// If the URI is not correctly formed, the Uri constructor
// will throw an exception.
ManualResetEvent waitObject;

Uri target = new Uri (args[0]);
string fileName = args[1];
FtpState state = new FtpState();
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(target);
request.Method = WebRequestMethods.Ftp.UploadFile;

// This example uses anonymous logon.
// The request is anonymous by default; the credential does not have to be specified.
// The example specifies the credential only to
// control how actions are logged on the server.

request.Credentials = new NetworkCredential ( "anonymous ", "janeDoe@contoso.com ");