第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何通過(guò) C# ASP.NET 使用 FTPWebRequest 下載“.xlsx”文件

如何通過(guò) C# ASP.NET 使用 FTPWebRequest 下載“.xlsx”文件

C#
慕容708150 2023-07-22 16:00:30
我已嘗試一切方法通過(guò) FTPWebRequest 下載 xlsx 文件,但無(wú)濟(jì)于事。我嘗試將文件類型從 xlsx 轉(zhuǎn)換為 xls 甚至 csv,但它只是給了我一個(gè)充滿難以辨認(rèn)符號(hào)的文件。所有文件類型都按預(yù)期通過(guò) Chrome 下載,提供的代碼也不例外,xlsx 文件除外。當(dāng)我嘗試使用此代碼下載 xlsx 文件時(shí),我從 Excel 中收到以下錯(cuò)誤:“我們發(fā)現(xiàn)‘filename.xlsx’中的某些內(nèi)容存在問(wèn)題。您希望我們盡可能恢復(fù)嗎?如果您信任此工作簿的來(lái)源,請(qǐng)單擊‘是’?!?單擊“是”后,會(huì)出現(xiàn)消息“Microsoft Excel 正在嘗試打開(kāi)并修復(fù)文件。要再次啟動(dòng)此過(guò)程,請(qǐng)從“打開(kāi)文件”對(duì)話框中選擇“打開(kāi)并修復(fù)”?!?繼續(xù)執(zhí)行這些指令只會(huì)創(chuàng)建相同消息的無(wú)限循環(huán)。任何對(duì)此的幫助都會(huì)很棒!另外,我嘗試將內(nèi)容類型更改為“application/vnd.openxmlformats-officedocument.spreadsheetml.sheet”和“application/vnd.ms-excel”,但都不起作用。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());
查看完整描述

1 回答

?
12345678_0001

TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超5個(gè)贊

您實(shí)際上并沒(méi)有告訴 FtpWebRequest 要下載哪個(gè)文件,ftpfilename 僅包含 FTP 服務(wù)器 URL。嘗試修改這一行:


FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpfilename));

到:


var requestUri = new Uri(ftpfilename + filename);

FtpWebRequest request = (FtpWebRequest)WebRequest.Create(requestUri);

我建議將您的 FtpWebRequest 代碼提取到一個(gè)帶有簽名的單獨(dú)方法中,public byte[] FtpDownloadBinary(Uri fromUri)您可以單獨(dú)實(shí)現(xiàn)和測(cè)試該簽名,然后您可以將該方法的結(jié)果傳遞到context.Response.BinaryWrite(fileBytes);


==編輯==


以下是 FTP 下載給定二進(jìn)制 URL 的示例方法:


string username = "anonymous";

string password = "anonymous@example.org";


public byte[] FtpDownloadBinary(Uri fromUri)

{

    FtpWebRequest request = (FtpWebRequest)WebRequest.Create(fromUri);

    request.Method = WebRequestMethods.Ftp.DownloadFile;

    request.Credentials = new NetworkCredential(username, password);

    request.UseBinary = true;

    request.KeepAlive = true;


    FtpWebResponse response = (FtpWebResponse)request.GetResponse();

    using (var responseStream = response.GetResponseStream())

    using (var memoryStream = new MemoryStream())

    {

        responseStream.CopyTo(memoryStream);

        return memoryStream.ToArray();

    }

}

您可以通過(guò)使用如下代碼將結(jié)果保存到系統(tǒng)上的本地文件來(lái)測(cè)試其是否有效,生成的文件可以使用 7-Zip、WinRAR、WinZip 等打開(kāi):


string ftpfilename = "ftp://ftp.ubuntu.com/ubuntu/ls-lR.gz"; //1.7MB example

var requestUri = new Uri(ftpfilename);

var fileBytes = FtpDownloadBinary(requestUri);


using (var fileStream = new FileStream("ls-lR.gz", FileMode.OpenOrCreate, FileAccess.Write, FileShare.None))

{

    fileStream.Write(fileBytes, 0, fileBytes.Length);

}


查看完整回答
反對(duì) 回復(fù) 2023-07-22
  • 1 回答
  • 0 關(guān)注
  • 211 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)