首页 新闻 赞助 找找看

附件下载,怎么就是不成功呢,没有保存对话框?

0
悬赏园豆:10 [已解决问题] 解决于 2014-01-08 08:44

前台

<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返回

Danny@yang的主页 Danny@yang | 初学一级 | 园豆:145
提问于:2014-01-03 18:00
< >
分享
最佳答案
0
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();
        }

试试这个。

收获园豆:3
水晶途途 | 小虾三级 |园豆:1443 | 2014-01-03 23:59

果断不行

Danny@yang | 园豆:145 (初学一级) | 2014-01-06 08:40
其他回答(2)
0

建议在调试模式下,一步步更着走,看看走到那里报错没?如果全部走下来全不报错,证明程序没问题,再看看是不是文件没找到?当然对应的JAR包一定是有的,我觉第一步就可能帮你解决问题!

收获园豆:2
godtrue | 园豆:242 (菜鸟二级) | 2014-01-03 18:29

没报什么错。。。

支持(0) 反对(0) Danny@yang | 园豆:145 (初学一级) | 2014-01-06 08:39
0

下载后的是文本你根本没写跳出对话框的代码,或者你可以不用AJAX直接POST回去就有对话框弹出了

收获园豆:5
56180825 | 园豆:1756 (小虾三级) | 2014-01-03 20:15

System.Web.HttpContext.Current.Response.AppendHeader("content-disposition", "online;filename=" + DownloadFile);//在线打开

这个不是吗?我看网上都用这种方法写的

支持(0) 反对(0) Danny@yang | 园豆:145 (初学一级) | 2014-01-06 08:38

@Danny@yang: 那是POST回去下载的代码,你用AJAX不能这样下载

支持(0) 反对(0) 56180825 | 园豆:1756 (小虾三级) | 2014-01-06 19:30

@56180825: 能不能详细点

支持(0) 反对(0) Danny@yang | 园豆:145 (初学一级) | 2014-01-07 08:20
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册