Winform下文件上传
在客户端定义一个文件上传的方法
/// <summary>
/// CS把指定的文件上载到服务器。
/// </summary>
///<param name="fullFileName"></param>
public void UpLoadFile(string fullFileName)
{
try
{
//得到cs上载文件处理页面
string upLoadPage = AppConfig.UpLoadPage;
if (upLoadPage == null || upLoadPage.Length < 1)
{
throw new Exception("缺少配置项!");
}
//检查要上传的文件是否存在
if (File.Exists(fullFileName))
{
WebClient webClient = new WebClient();
//webClient.Headers.Add(
byte [] byteResponse=webClient.UploadFile(upLoadPage,fullFileName);
//取得服务器端文件名称
fullName = Encoding.Default.GetString(byteResponse); //fullFileName;
}
else
{
throw new Exception("要上传的文件不存在!");
}
}
catch (Exception ex)
{
throw ex;
}
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using OPSS.Logic.FileManage;
using OPSS.Public.SysFrame;
public partial class Include_DocUpLoad : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
try
{
string fileName = Request.Files[0].FileName;
string rootPath = AppConfig.RootPath;
//检查配置项是否存在
if (AppConfig.RootPath.Length < 1 || AppConfig.RootPath.LastIndexOf(@"\") == (AppConfig.RootPath.Length - 1))
{
throw new Exception(@"缺少目录配置项或目录配置项以\结尾");
}
//检查存储目录是否存在,如果不存在创建目录。
if (!Directory.Exists(rootPath))
{
Directory.CreateDirectory(rootPath);
}
string fullFileName = AppConfig.RootPath+"\\"+DateTime.Now.ToString("yyyyMMddHHmmss")+fileName;
//如果文件存在会覆盖。
Request.Files[0].SaveAs(fullFileName);
Response.Write(fullFileName);
Response.End();
}
catch (Exception ex)
{
}
}
}


浙公网安备 33010602011771号