public static void TransmitFile(Page CurrentPage, string FilePath)
// CurrentPage = this when called from CodeBehind
{
if (File.Exists(FilePath))
{
string FileName = Path.GetFileName(FilePath);
string Extension = Path.GetExtension(FilePath).ToLowerInvariant();
string ContentType;
switch (Extension)
{
case "xlsx":
ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
break;
case "xls":
ContentType = "application/vnd.ms-excel";
break;
case "csv":
ContentType = "text/csv";
break;
default:
ContentType = "application/octet-stream";
break;
}
if (CurrentPage != null)
{
CurrentPage.Response.ContentType = ContentType;
CurrentPage.Response.AppendHeader("Content-Disposition", "attachment; filename=" + FileName);
CurrentPage.Response.TransmitFile(FilePath);
CurrentPage.Response.End();
}
else
throw new Exception("File transmission failed: cannot access current page");
}
else
throw new Exception("File transmission failed: file not found");
}
Ask for location for saving downloaded file
-
30-09-2022 - |
Domanda
I am writting a code for downloading an excel file in the local system.
Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbooks workBooks = excelApp.Workbooks;
Microsoft.Office.Interop.Excel.Workbook workBook = workBooks.Open(@"D:\Myfile.xlsx");
// Microsoft.Office.Interop.Excel.Worksheet workSheet = workBook.Worksheets.get_Item(1);
workBook.SaveCopyAs(@"D:\Copy_Myfile.xlsx");
workBook.Close();
Now the requirement is instead of saving the file to D drive, it should ask for the user to save the file to desied location.
Any idea?
Soluzione
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow