表单提交上传文件代码(Ext,C#)
2010-12-31 08:46
411 查看
页面代码(嵌入在EXJjs中):
上传文件,C#后台:
下载文件,C#后台:
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) { } } }
相关文章推荐
- C#后台代码 Post 提交表单上传文件及其他信息
- Asp.Net模拟表单提交数据和上传文件的实现代码
- 通过表单提交数据,完成文件上传代码
- Servlet实现文件上传到服务器(不用表单,直接用代码模拟表单提交)
- Ajax提交Form表单及文件上传的实例代码
- c# 模拟表单提交,post form 上传文件、大数据内容
- Multipart/form-data POST文件上传详解 理论 简单的HTTP POST 大家通过HTTP向服务器发送POST请求提交数据,都是通过form表单提交的,代码如下: <form me
- C#模拟form表单提交数据,上传文件
- c# 模拟提交带文件上传的表单
- Asp.Net模拟表单提交数据和上传文件的实现代码
- android 表单提交 (支持多文件上传) + 服务端代码
- c# 模拟表单提交,post form 上传文件、大数据内容
- Java中,当表单含有文件上传时,提交数据的如何读取
- Ajax方式提交带文件上传的表单
- C# 完整上传文件 代码
- 使用jquery-form.js异步上传文件和提交表单
- 使用ajax提交form表单,包括ajax文件上传
- struts2.x表单重复提交,文件上传下载,转换器
- cos与EXT调用AJAX提交上传文件
- 安卓之上传文件,即HTTP提交表单