简体   繁体   中英

Saving an excel file to users PC using save as Dialog box

Say I have a server that returns a link. the link is a link to an excel file. Now I want to download that file and save it to my local pc by poping a save dailog box.

I have the following codes but it's not working. It's not poping up the save dialog box and it's not saving the file.

    try
    {
        string filepath = Server.MapPath("~/Images/0ca66926-6977-43d3-9c97-f84a43f6ce5d.xls");      // supply the return link
        FileInfo myfile = new FileInfo(filepath);
        if (myfile.Exists)
        {
            Response.ClearContent();
            Response.AddHeader("Content-Disposition", "attachment; filename=" + myfile.Name);
            Response.AddHeader("Content-Length", myfile.Length.ToString());
            Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
            Response.WriteFile(myfile.FullName);
            Response.End();
        }
    }
    catch (Exception e)
    {
        Console.WriteLine(e.Message);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM