Как скачать файл с сервера в веб-методе ?
I do not know how to achieve to download file from server using web method. I tried something like this. I passed data and file name from angularJS to my web method, it creates excel file and upload in server. after completion of file creation and upload in server i returned url to another webform on success call to my JS function. From there I tried to open new web form where i am trying to implement code to download file. But how would my code will know which life to download since there could be multiple file presented. how so i pass fileName as well so that i could identified the file to download. Please check my code and let me know what to change and what to do ? or anyother way to achieve this. Thank you.
Что я уже пробовал:
[WebMethod] public static string ExportExcel(List<Student> students, string fileName) { ListtoDataTableConverter converter = new ListtoDataTableConverter(); DataTable dt = converter.ToDataTable(students); DataSet ds = new DataSet(); ds.Tables.Add(dt); string FilePath = fileLocation + fileName + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".xlsx"; ; DataSetToExcel.ExportDataSet(FilePath, ds); HttpContext.Current.Response.ContentType = "application/octet-stream"; HttpContext.Current.Response.AppendHeader("Content-Disposition", "filename=" + Path.GetFileName(FilePath)); return JsonConvert.SerializeObject(new { Success = true, redirectUrl = VirtualPathUtility.ToAbsolute("~/DownloadExcelForm.aspx") }); } Javascript: $scope.SendFile = function (filteredItems) { var xlFile = $scope.xlFilename; var students = []; students = filteredItems ; $http({ method: "POST", url: "ReportStudent.aspx/ExportExcel", data: { students: students, fileName: xlFile }, contentType: "application/json", dataType: "json" }).then(function (response) { var responsedata = JSON.parse(response.data.d); window.location.href = responsedata.redirectUrl; }); }; Webform Pageload: protected void Page_Load(object sender, EventArgs e) { string filepath = ""; Response.ContentType = ContentType; // Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(FilePath)); how to pass this file path ???? Response.TransmitFile(FilePath); Response.Flush(); System.Threading.Thread.Sleep(1000); System.Threading.Thread.Sleep(1000); if (File.Exists(filepath)) { File.Delete(filepath); } //response.writefile(filepath) Response.End(); }