您的位置:首页 > 编程语言 > C#

表单提交上传文件代码(Ext,C#)

2010-12-31 08:46 411 查看
页面代码(嵌入在EXJjs中):

html: '<br/><br/><form target="hidden_frame"  method="post" action="UploadCIexcel.aspx" enctype="multipart/form-data"><table><tr><td  width="90" align="right">请选择文件:</td><td><input type="file" runat="serever" style="width:250;height:20" name="upfile"  id="upfile" /></td></tr><tr height="90"><td align="right"><a href="Export/CiDownloadExample.aspx" mce_href="Export/CiDownloadExample.aspx" style="color:blue" mce_style="color:blue">下载模板</a></td><td align="right"><input type="submit" style="height:20;width:65" value="提交"></td></tr></table><iframe name="hidden_frame" id="hidden_frame" style="display: none;" mce_style="display: none;" ></iframe></form>


上传文件,C#后台:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.IO;
using BaseClass;
using System.Data.OleDb;

public partial class freekpiselect_uploadCIexcel : System.Web.UI.Page
{
string uploadFolder = "../upload"; // 上传文件夹
protected void Page_Load(object sender, EventArgs e)
{
HttpFileCollection files = Request.Files;

if (files.Count == 0)
{
Response.Write("<mce:script type="text/javascript"><!--
parent.callbackForUpload('false')
// --></mce:script>");
Response.End();
}

/// Response.Write("请勿直接访问本文件");
string path = Server.MapPath(uploadFolder);

// 只取第 1 个文件
HttpPostedFile file = files[0];
try
{
if (file != null && file.ContentLength > 0)
{
string fileName=file.FileName;//获取文件名
if (fileName.Substring(fileName.Length - 4).ToUpper() != ".XLS")
{
Response.Write("<mce:script type="text/javascript"><!--
parent.callbackForUpload('notxls')
// --></mce:script>");
}
else
{
string savePath = path + "/" + "tempCI.xls";
if (File.Exists(savePath))
{
File.Delete(savePath);
}
file.SaveAs(savePath);

DataSet sXmlds = ExcelToXml(savePath); //Global.ExcelHelper.ExcelToDataSet(savePath);//
DataTable dtAll = sXmlds.Tables[0];
string sCI = ",";
for (int i = 0; i < dtAll.Rows.Count; i++)
{
if (dtAll.Rows[i][0] != null && dtAll.Rows[i][0].ToString() != "")
{
sCI += "," + dtAll.Rows[i][0];
}
}
sCI = sCI.Substring(1);
Response.Write("<mce:script type="text/javascript"><!--
parent.callbackForUpload('" + sCI + "')
// --></mce:script>");
}
Response.End();
}
else
{
Response.Write("<mce:script type="text/javascript"><!--
parent.callbackForUpload('nofiles')
// --></mce:script>");
}
}
catch (Exception ex)
{
}

}
public DataSet ExcelToXml(string sPath)
{
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + sPath + ";" + "Extended Properties=Excel 8.0;";
// OleDbConnection conn = new OleDbConnection(strConn);
DataSet ds = null;
using (OleDbConnection conn = new OleDbConnection(strConn))
{
conn.Open();
string strExcel = "";
OleDbDataAdapter myCommand = null;

strExcel = "select * from [sheet1$]";
myCommand = new OleDbDataAdapter(strExcel, strConn);
ds = new DataSet();
myCommand.Fill(ds, "table1");
conn.Close();
}
File.Delete(sPath);
return ds;
}
}


下载文件,C#后台:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using BaseClass;
using System.IO;

public partial class freekpiselect_export_ciDownloadExample : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string filePath = Server.MapPath("../../upload/ExcelTemplate") + "/" + "CIExample.xls"; ;

DownLoadFile(filePath);
}
//下载函数
public void DownLoadFile(string filePath)
{
try
{
if (File.Exists(filePath))
{
FileInfo file = new FileInfo(filePath);
Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8"); //解决中文乱码
Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name)); //解决中文文件名乱码
Response.AddHeader("Content-length", file.Length.ToString());
Response.ContentType = "appliction/octet-stream";
Response.WriteFile(file.FullName);
Response.End();
}
}
catch (Exception e)
{
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: