I've tried everything to get an xlsx file to download through an FTPWebRequest -- to no avail. I've tried converting the file type from xlsx to xls and even to csv, but it just gives me a file full of indecipherable symbols. All file types download through Chrome as expected with no exceptions with the code provided, except for xlsx files.
When I try to download an xlsx file using this code, I get the following error from excel: "We found a problem with some content in 'filename.xlsx'. Do you want us to recover as much as we can? If you trust the source of this workbook, click 'Yes'." Upon clicking on 'Yes', the message "Microsoft Excel was attempting to open and repair the file. To start this process again, choose Open and Repair from the Open file dialog." Proceeding with those instructions just creates an endless loop of the same messages.
Any help on this would be great! Also, I've tried changing the content-type to "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" and "application/vnd.ms-excel", and neither works.
string ftpfilename = "ftp://ftpserverinfo/randomfilename.xlsx";
string filename = "randomfilename.xlsx";
FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpfilename));
request.Method = WebRequestMethods.Ftp.DownloadFile;
request.Credentials = new NetworkCredential(username, password);
request.UseBinary = true;
request.KeepAlive = true;
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Stream stream = response.GetResponseStream();
byte[] bytes = new byte[2048];
int i = 0;
MemoryStream mStream = new MemoryStream();
do { i = stream.Read(bytes, 0, bytes.Length); mStream.Write(bytes, 0, i); } while (i != 0);
context.Response.Clear();
context.Response.ClearHeaders();
context.Response.ClearContent();
context.Response.ContentType = MimeMapping.GetMimeMapping(filename);
context.Response.AddHeader("content-disposition", @"attachment;filename=" + filename);
context.Response.BinaryWrite(mStream.GetBuffer());