我现在做一个B/S的程序,想做一个上传功能,点击button1按钮后,将一个动态生成的文件保存成文本test.tex后再上传至另外一台电脑,我的程序放在服务器比如是192.168.2.6,而我现在要上传至192.168.2.5,而我现在是在客户端操作,如何办。高手麻烦提示一下。(其实文本文件里面是动态生成的html代码)
解决方案 »
- 求WinForm程序解析aspx里有多少个控件
- 不要定义可见的实例域变量
- 用WinForm(C#)编写一个应用程序,将一个扩展名为EXE文件移动到另一个文件夹下
- web 中 时间.ToString()的参数问题
- 如何得到指定月份的第一天和最后一天,
- DataGrid固定列的问题?
- 讨论一下在线升级功能的实现,如何在主程序退出时启动升级程序
- visual studio 2013中 添加不了zedgraph控件,求助〉〉
- 当我按ALT+S时触发我的一个按纽事件,怎么实现.
- 有哪位xdjm用C#做图象处理的?盼能相互交流。
- 在另一个窗口增加另一个窗口的tabcontrol控件的page
- 跪求解决。顶着有分
客户端代码
string filePath = txtFilePath.Text.Trim();
try
{
if (filePath != null && txtBacPath.Text.Trim() != null)
{
if (Directory.Exists(filePath))
{
//获取该文件夹下所有文件路径
string[] files = Directory.GetFiles(filePath);
foreach (string file in files)
{
WebClient client = new WebClient();
//上载文件
client.UploadFile("http://192.168.1.200:7000/download/DownLoad/UploadFile.aspx", "POST", file);
}
}
}
}
catch (Exception error)
{
} 服务端代码
try
{
//判断文件夹是否存在
if (!Directory.Exists(HttpRuntime.AppDomainAppPath + @"DownLoad\Files\" + DateTime.Now.Year.ToString() + "\\" + DateTime.Now.Month.ToString() + "\\" + DateTime.Now.Day.ToString() + "\\"))
{
//文件夹不存在,创建文件夹
Directory.CreateDirectory(HttpRuntime.AppDomainAppPath + @"DownLoad\Files\" + DateTime.Now.Year.ToString() + "\\" + DateTime.Now.Month.ToString() + "\\" + DateTime.Now.Day.ToString() + "\\");
}
foreach (string file in Request.Files.AllKeys)
{
//获取文件
HttpPostedFile hpFile = Request.Files[file];
//文件路径
string filePath = HttpRuntime.AppDomainAppPath + @"DownLoad\Files\" + DateTime.Now.Year.ToString() + "\\" + DateTime.Now.Month.ToString() + "\\" + DateTime.Now.Day.ToString() + "\\" + hpFile.FileName;
//保存文件
hpFile.SaveAs(filePath); }
}
catch (Exception error)
{
//异常日志
}
}
客户端代码相当于,你要实现上传功能的页面,服务端代码就是你发布的一个页面也就是
http://192.168.1.200:7000/download/DownLoad/UploadFile.aspx
能不能再解释一下。
<input id="File1" type="file" runat="server" />
<asp:Button ID="Button2" runat="server" onclick="Button2_Click1"
Text="Button" />
</form> Button2_Click1事件这么处理
protected void Button2_Click1(object sender, EventArgs e)
{
for (int i = 0; i < Request.Files.Count; i++)
{
string UserPath = Server.MapPath("Image").ToString();
//如果文件夹不存在则创建
if (!System.IO.Directory.Exists(UserPath))
{
System.IO.Directory.CreateDirectory(UserPath);
} string fileName2 = System.IO.Path.GetFileName(Request.Files[i].FileName);
//Response.Write(UserPath + "\\" + fileName2); Request.Files[i].SaveAs(UserPath + "\\" + fileName2); }
}
处加上这个方法 if( Upload_Client(http://192.168.2.5/default.aspx,UserPath + "\\" + fileName2) )
File.Delete(UserPath + "\\" + fileName2);
/// <summary>
/// 将本地文件上传到指定的服务器(WebClient方法)
/// </summary>
/// <param name="address">文件上传到的服务器</param>
/// <param name="fileNamePath">要上传的本地文件(全路径)</param>
/// <returns>成功返回1,失败返回0</returns>
public static int Upload_Client(string address, string fileNamePath)
{
WebClient wc = new WebClient();
FileStream fs = new FileStream(fileNamePath, FileMode.Open, FileAccess.Read);
address = string.Concat(address, "?filename=", Path.GetFileName(fileNamePath));
Stream poststream = wc.OpenWrite(address, "POST");
byte[] buffer = new byte[1024];
int mum = (int)fs.Length;
//已上传的字节数
long offset = 0;
int length = fs.Read(buffer, 0, buffer.Length);//读取长度
//开始上传时间
DateTime startTime = DateTime.Now;
while (length > 0)
{
poststream.Write(buffer, 0, length);
offset += length;
TimeSpan span = DateTime.Now - startTime;
double second = span.TotalSeconds;
Console.WriteLine("已用时:" + second.ToString("F2") + "秒");
if (second > 0.1)
{
Console.WriteLine(" 平均速度:" + (offset / 1024 / second).ToString("0.00") + "KB/秒");
}
else
{
Console.WriteLine(" 正在连接…");
}
Console.WriteLine("已上传:" + (offset * 100.0 / mum).ToString("F2") + "%");
Console.WriteLine((offset / 1048576.0).ToString("F2") + "M/" + (mum / 1048576.0).ToString("F2") + "M");
Application.DoEvents();
length = fs.Read(buffer, 0, buffer.Length);
}
poststream.Close();
if (wc.QueryString.ToString() == "Success")
return 1;
else
return 0;
}然后 在192.168.2.5中,Default.aspx文件
protected void Page_Load(object sender, EventArgs e)
{ReceiveFile();} private void ReceiveFile()
{
System.IO.Stream sr = Request.InputStream;
if (sr.Length > 0)
{
string filename = Request.QueryString["filename"];
int size = 1024;
byte[] by = new byte[size];
int length = sr.Read(by, 0, size);
System.IO.FileStream fs = new System.IO.FileStream(Path.Combine(Server.MapPath("Image"), filename), System.IO.FileMode.Create, System.IO.FileAccess.Write);
while (length > 0)
{
fs.Write(by, 0, by.Length);
length = sr.Read(by, 0, size);
}
fs.Close();
sr.Close();
Response.Write("Success\r\n");
} if (Request.Files.Count > 0)
{
try
{ HttpPostedFile file = Request.Files[0];
string filePath = Server.MapPath("\\Image\\" + file.FileName);
file.SaveAs(filePath);
Response.Write("Success\r\n");
}
catch
{
Response.Write("Error\r\n");
}
} }