private void Form1_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("server=.;uid=sa;pwd=;database=jinjiedb");
con.Open();
SqlCommand cmd = new SqlCommand("select username from users", con);
DataTable dt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(dt);
dataGridView1.DataSource = dt;
}
public static bool UploadFile(string localFilePath, string serverFolder,bool reName)
{
string fileNameExt, newFileName, uriString;
if (reName)
{
fileNameExt = localFilePath.Substring(localFilePath.LastIndexOf(".") + 1);
newFileName = DateTime.Now.ToString("yyMMddhhmmss") + fileNameExt;
}
else
{
newFileName = localFilePath.Substring(localFilePath.LastIndexOf("\\")+1);
}
if (!serverFolder.EndsWith("/") && !serverFolder.EndsWith("\\"))
{
serverFolder = serverFolder + "/";
} uriString = serverFolder + newFileName; //服务器保存路径
/**//// 创建WebClient实例
WebClient myWebClient = new WebClient();
myWebClient.Credentials = CredentialCache.DefaultCredentials; // 要上传的文件
FileStream fs = new FileStream(newFileName, FileMode.Open, FileAccess.Read);
BinaryReader r = new BinaryReader(fs);
try
{
//使用UploadFile方法可以用下面的格式
//myWebClient.UploadFile(uriString,"PUT",localFilePath);
byte[] postArray = r.ReadBytes((int)fs.Length);
Stream postStream = myWebClient.OpenWrite(uriString, "PUT");
if (postStream.CanWrite)
{
postStream.Write(postArray, 0, postArray.Length);
}
else
{
MessageBox.Show("文件目前不可写!");
}
postStream.Close();
}
catch
{
//MessageBox.Show("文件上传失败,请稍候重试~");
return false;
} return true;
}这个是我从网上找的,具体怎么用 不清楚
望大侠指点一下
MSN [email protected]
谢谢大侠们
private void button1_Click(object sender, EventArgs e)
{
openFileDialog1.ShowDialog();
UploadFile(openFileDialog1.FileName, "d:\\1", false);
}
public static bool UploadFile(string localFilePath, string serverFolder, bool reName)
{
string fileNameExt, newFileName, uriString;
if (reName)
{
fileNameExt = localFilePath.Substring(localFilePath.LastIndexOf(".") + 1);
newFileName = DateTime.Now.ToString("yyMMddhhmmss") + fileNameExt;
}
else
{
newFileName = localFilePath.Substring(localFilePath.LastIndexOf("\\") + 1);
}
if (!serverFolder.EndsWith("/") && !serverFolder.EndsWith("\\"))
{
serverFolder = serverFolder + "/";
} uriString = serverFolder + newFileName; //服务器保存路径
/**/
/// 创建WebClient实例
WebClient myWebClient = new WebClient();
myWebClient.Credentials = CredentialCache.DefaultCredentials; // 要上传的文件
FileStream fs = new FileStream(localFilePath, FileMode.Open, FileAccess.Read);
// FileStream fs = new FileStream(newFileName, FileMode.Open, FileAccess.Read);
BinaryReader r = new BinaryReader(fs);
try
{
//使用UploadFile方法可以用下面的格式
//myWebClient.UploadFile(uriString,"PUT",localFilePath);
byte[] postArray = r.ReadBytes((int)fs.Length);
// Stream postStream = myWebClient.OpenWrite(uriString, "PUT");
Stream postStream = myWebClient.OpenWrite(uriString, "PUT");
if (postStream.CanWrite)
{
postStream.Write(postArray, 0, postArray.Length);
}
else
{
return false;
}
postStream.Close();
}
catch
{
// //MessageBox.Show("文件上传失败,请稍候重试~");
return false;
} return true;
}这样能传到本地d盘文件夹1下面
如何上传到远程服务器上呢
把"d:\\1"换成ftp://123.456.789.20/wtt为什么就不行呢?
当然这个ftp是个假的
一般FTP服务器都需要密码的,这样做不行。
myWebClient.Credentials =new NetworkCredential(username,password);//对应ftp服务器上的用户名和密码
String uriString = Console.ReadLine();// Create a new WebClient instance.
WebClient myWebClient = new WebClient();
myWebClient.Credentials =new NetworkCredential(username,password);//对应ftp服务器上的用户名和密码Console.WriteLine("\nPlease enter the fully qualified path of the file to be uploaded to the URI");
string fileName = Console.ReadLine();
Console.WriteLine("Uploading {0} to {1} ...",fileName,uriString);// Upload the file to the URI.
// The 'UploadFile(uriString,fileName)' method implicitly uses HTTP POST method.
byte[] responseArray = myWebClient.UploadFile(uriString,fileName);// Decode and display the response.
Console.WriteLine("\nResponse Received.The contents of the file uploaded are:\n{0}",
System.Text.Encoding.ASCII.GetString(responseArray));
只是从本地用流读入文件上传到服务器
string password="********T";
string uri = "ftp://******/wtt/UnicodeDemo.cs";
string fileName = "C:\\UnicodeDemo.cs";
WebClient client = new WebClient();
client.Credentials = new NetworkCredential(username, password);
byte[] result=null;
try
{
result = client.UploadFile(uri, fileName);
}
catch (WebException exc)
{
MessageBox.Show(exc.Status.ToString());
}
string resultString = System.Text.Encoding.ASCII.GetString(result);
MessageBox.Show(resultString);是可以的。