文件下载

    xiaoxiao2021-03-25  97

    前面已经普及了Nancy的入门知识,接下来讲一下文件的下载:

    //多文件下载 Get["/Download/"] = Post["/Download/"] = p => { try { string ID = Request.Query["ID"].Value;//这句是为了接收前台抛来的参数,如果没有参数也可以不写 if (string.IsNullOrEmpty(IDs)) { return Response.AsText("文件下载失败:参数为空").WithHeader("Acess-Control-Allow-Origin", "*"); } var word_data = new word_data();//实例化要用方法的函数 var wordPath = word_data.batchDownload(ID);//调用生成文档的方法,返回文档的地址 return Response.AsFile(wordPath, "application/vnd.ms-word").WithHeader("Access-Control-Allow-Origin", "*").WithHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(Path.GetFileName(wordPath), System.Text.Encoding.UTF8)); } catch (Exception e) { return Response.AsText("文件下载失败" + e.Message).WithHeader("Acess-Control-Allow-Origin", "*"); } };这里需要说一下,这是文件下载,所以不论是word还是pdf或者是Excel,都可以给个路径下载

    转载请注明原文地址: https://ju.6miu.com/read-22041.html

    最新回复(0)