前台
<tr>
<td>上传附件</td>
<td colspan="2">
<input type="file" id="uploadFile" name="uploadFile" /> <br/>
<p style="margin-top:5px;font-size:14px;font-weight:bold;"><span id="result2"></span></p>
<br />
<input type="button" value ="下载附件"id="downloadFile" onclick="downloadFile()"/>
</td>
</tr>
//下载附件
function downloadFile() {
var filename = $("#result2").text();
alert(filename);
$.ajax({
type: "POST",
dataType: "json",
url: "/OrderManage/DownLoadFile?filename=" + filename,
data: "",
error: function (msg) { alert("下载失败"); },
success: function (msg) {
alert("下载成功");
}
});
}
后台
//下载附件
[RoleFilter()]
public ActionResult DownLoadFile(string filename)
{
int userID = Convert.ToInt32(Request.Cookies["UserID"].Value.ToString());//获取登陆账号ID
UserInfo currentUser = userInfoDAL.GetModelByID(userID);//通过UserInfo表,点方法(id)
int AreaId = Convert.ToInt32(currentUser.AreaID);//用户名id
AreaDraw entiy = new AreaDraw();
AreaDraw tmp = areaDrawDAL.GetAreaDrawByAreaID(AreaId);
entiy.AreaID = AreaId;
String path = "";
//文件所在项目中的位置
// string filename1 = "2013-12-31-16-44-9.docx";
path = System.Web.HttpContext.Current.Server.MapPath("~/UploadFiles/" + filename);
FileInfo DownloadFile = new FileInfo(path);
//附件保存本机
if (DownloadFile.Exists)
{
System.Web.HttpContext.Current.Response.Clear();
System.Web.HttpContext.Current.Response.ClearHeaders();
//System.Web.HttpContext.Current.Response.Buffer = true;
System.Web.HttpContext.Current.Response.ContentType = "application/octet-stream";
System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(DownloadFile.FullName, System.Text.Encoding.UTF8));
System.Web.HttpContext.Current.Response.AppendHeader("Content-Length", DownloadFile.Length.ToString());//附件下载
System.Web.HttpContext.Current. Response.AddHeader("Content-Transfer-Encoding", "binary");
System.Web.HttpContext.Current.Response.AppendHeader("content-disposition", "online;filename=" + DownloadFile);//在线打开
//Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
System.Web.HttpContext.Current.Response.WriteFile(DownloadFile.FullName);
System.Web.HttpContext.Current.Response.Flush();
System.Web.HttpContext.Current.Response.End();
}
else
{
Response.Write("文件不存在");
Response.End();
}
return Json(path, JsonRequestBehavior.AllowGet);
}
点击按钮,触发事件,将文件名传到控制器中的 Public ActionResult DownLoadFile(string fileName)这个方法名,处理后再以json返回
public FileResult DownloadSqLiteDb(string FileName) { string FilePath = AppDomain.CurrentDomain.BaseDirectory + "SQLiteDb\\" + FileName + ".db"; if (System.IO.File.Exists(FilePath)) { return File(System.IO.File.ReadAllBytes(FilePath), System.Net.Mime.MediaTypeNames.Application.Octet, FileName + ".db"); } throw new System.IO.FileNotFoundException(); }
试试这个。
果断不行
建议在调试模式下,一步步更着走,看看走到那里报错没?如果全部走下来全不报错,证明程序没问题,再看看是不是文件没找到?当然对应的JAR包一定是有的,我觉第一步就可能帮你解决问题!
没报什么错。。。
下载后的是文本你根本没写跳出对话框的代码,或者你可以不用AJAX直接POST回去就有对话框弹出了
System.Web.HttpContext.Current.Response.AppendHeader("content-disposition", "online;filename=" + DownloadFile);//在线打开
这个不是吗?我看网上都用这种方法写的
@Danny@yang: 那是POST回去下载的代码,你用AJAX不能这样下载
@56180825: 能不能详细点