下载所有类型的文件使用ASP.NET服务器 [英] Download all types of files from server using ASP.NET

查看:115
本文介绍了下载所有类型的文件使用ASP.NET服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在服务器中的文件。

我要下载此文件。

我用这个code

try
    {
        Response.Clear();
        Response.ClearContent();
        Response.ClearHeaders();
        Response.BufferOutput = true;

        if (File.Exists(Server.MapPath("~/Upload/" + file)))
        {
            Response.AddHeader("Content-Disposition", "attachment; filename=" + file);
            Response.ContentType = "application/octet-stream";
            Response.WriteFile(("~/Upload/" + file));
            Response.End();
            HttpContext.Current.ApplicationInstance.CompleteRequest();
        }
        else
        {
            if (Request.UrlReferrer != null)
            {
                Type csType = GetType();
                string jsScript = "alert('File Not Found');";
                ScriptManager.RegisterClientScriptBlock(Page, csType, "popup", jsScript, true);
            }
        }
    }
    catch (Exception ex)
    {
        string errorMsg = ex.Message;
        ScriptManager.RegisterClientScriptBlock(Page, GetType(), "popup", errorMsg, true);
    }

但是,当我用这个,我得到错误

But when i use this, I get error

,因此无法在code优化或本机框架是在调用堆栈的顶部,以评估前pression。

Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.

在code

到Response.End();

Response.End();

如何下载的所有文件类型?

How to download all types of files?

推荐答案

你试过:

Response.TransmitFile(Server.MapPath("~/Upload/" + file));

而不是:

Response.WriteFile(("~/Upload/" + file));

您也应该删除与BufferOutput(坏主意到Response.End()没有 Response.Flush()

You should also remove the BufferOutput (bad idea with Response.End() without Response.Flush())

这篇关于下载所有类型的文件使用ASP.NET服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆