webclient访问本地html,C# winform 文件浏览选择上传至本地服务器HttpWebClient+调用本地文件夹...

在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);

}

}

}

0818b9ca8b590ca3270a3433284dd417.png

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;

}

*/

}

}

目前,只实现了上传文件到本地,还不支持本地/局域网服务器 文件下载。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值