服务器端 新建aspx页面,部署网站:http://localhost:4944/UploadFileWebSite/Default.aspx
在Page_Load写入代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
foreach (string f in Request.Files.AllKeys)
{
HttpPostedFile file = Request.Files[f];
file.SaveAs(@"d:\TestFileUP\" + file.FileName);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Text;
namespace FileUD
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//private void UpLoadFile1(string fileNamePath, string uriString)
//{
// //string fileName = fileNamePath.Substring(fileNamePath.LastIndexOf("\\") + 1);
// string NewFileName = DateTime.Now.ToString("yyMMddhhmmss") + DateTime.Now.Millisecond.ToString() + fileNamePath.Substring(fileNamePath.LastIndexOf("."));
// string fileNameExt = fileNamePath.Substring(fileNamePath.LastIndexOf(".") + 1);
// if (uriString.EndsWith("/") == false) uriString = uriString + "/";
// uriString = uriString + NewFileName;
// /**/
// /// 创建WebClient实例
// WebClient myWebClient = new WebClient();
// myWebClient.Credentials = CredentialCache.DefaultCredentials;
// // 要上传的文件
// FileStream fs = new FileStream(fileNamePath, FileMode.Open, FileAccess.Read);
// //FileStream fs = OpenFile();
// BinaryReader r = new BinaryReader(fs);
// try
// {
// //使用UploadFile方法可以用下面的格式
// //myWebClient.UploadFile(uriString,"PUT",fileNamePath);
// 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("文件上传失败,请稍候重试~");
// }
//}
//浏览文件
private void btnLL_Click(object sender, EventArgs e)
{
//this.openFileDialog1.Filter = "jpg文件(*.jpg)|*.jpg|gif文件(*.gif)|*.gif";
this.openFileDialog1.Filter = "所有文件(*.*)|*.*";
if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
{
/*
string PicFileName = this.openFileDialog1.FileName;
this.imgList.Add(PicFileName);
this.imageList1.Images.Add(Image.FromFile(PicFileName));
*/
string FileName = this.openFileDialog1.FileName;
txtFileLJ.Text = FileName;
}
}
///WebClient的UploadFile方法
public bool uploadFileByHttp(string webUrl,string localFileName)
{
// 检查文件是否存在
if (!System.IO.File.Exists(localFileName))
{
MessageBox.Show("请选择需要上传的文件!",localFileName);
return false;
}
try
{
System.Net.WebClient myWebClient = new System.Net.WebClient();
myWebClient.UploadFile(webUrl, "POST", localFileName);
MessageBox.Show("上传成功!", "上传状态:", MessageBoxButtons.OKCancel);
txtFileLJ.Text = "";
}
catch
{
return false;
}
finally
{
MessageBox.Show("文件上传出现异常,未能上传成功!");
}
return true;
}
//本地上传文件
private void btnUpLoad_Click(object sender, EventArgs e)
{
//调用ftpUP类的方法UploadFile(FileInfo fileinfo, string targetDir, string hostname, string username, string password, string fileName)
/// 需要上传的文件
/// 目标路径
/// ftp地址
/// ftp用户名
/// ftp密码
///
//ftpUP ftp = new ftpUP();
//ftp.UploadFile();
//UpLoadFile1(txtFileLJ.Text, "192.168.1.104");
//uploadimg();
//调用UploadFile方法
//UploadFile(txtFileLJ.Text, txtFileLJ.Text, true);
this.uploadFileByHttp("http://localhost:4944/UploadFileWebSite/Default.aspx", txtFileLJ.Text);
}
///
/// 浏览文件夹
///
///
///
private void btnShowFileInfo_Click(object sender, EventArgs e)
{
folderBrowserDialog1.RootFolder = Environment.SpecialFolder.Desktop;
folderBrowserDialog1.Description = "请选择文件夹";
DialogResult d = folderBrowserDialog1.ShowDialog();
if (d == DialogResult.OK)
{
//txtFilesName指的是界面一个文本框获取路径
txtFilesName.Text = folderBrowserDialog1.SelectedPath;
}
else
txtFilesName.Text = "请选择目录!";
}
///
/// 退出程序
///
///
///
private void btnClear_Click(object sender, EventArgs e)
{
Application.Exit();
}
///
/// 浏览服务器的文件路径
///
///
///
private void btnServerLL_Click(object sender, EventArgs e)
{
//显示目录路径
System.Diagnostics.Process.Start("Explorer.exe", @"\\192.168.1.1\d$\公共区\");
}
///
/// 调用资源管理器
///
///
///
private void button1_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("Explorer.exe", @"d:\TestFileUP\");
}
/*
public void uploadimg()
{
FileInfo fileInf = new FileInfo(txtFileLJ.Text);
string uri = @"http://www.rnkeysoft.com/"+txtFileLJ.Text.Substring(txtFileLJ.Text.LastIndexOf('\\'));
HttpWebRequest reqHttp;
reqHttp = (HttpWebRequest)HttpWebRequest.Create(uri);
//reqHttp.UseBinary = true;
reqHttp.Credentials = new NetworkCredential("imagupload", "123456");//ftp用户名和密码
reqHttp.KeepAlive = false;
reqHttp.Method = WebRequestMethods.Ftp.UploadFile;
reqHttp.ContentLength = fileInf.Length;
int buffLength = 2048;
byte[] buff = new byte[buffLength];
int contentLen;
FileStream fs = fileInf.OpenRead();
Stream strm = reqHttp.GetRequestStream();
contentLen = fs.Read(buff, 0, buffLength);
while (contentLen != 0)
{
strm.Write(buff, 0, contentLen);
contentLen = fs.Read(buff, 0, buffLength);
}
strm.Close();
fs.Close();
}
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;
}
*/
}
}