【解決方法】ハイパーリンクを使用してブラウザでファイルを開く方法


パブリック文字列 GetCheckListDocUrl(オブジェクト成果物ID)
{

ROOM objScope = 新しい ROOM();
objScope.DelivarablesID = Convert.ToInt32(DeliverablesID);
DataSet dsDelivarablesDoc = objScope.GetSODeliverablesDocument();
// スコープDoc = dsDelivarablesDoc.Tables[0].Rows[0][“PLC_Filepath”].ToString();
文字列 siteurl = System.Configuration.ConfigurationManager.AppSettings[“websiteroot”].ToString();
文字列パス = DeliverablesID.ToString().Replace(“~”, “”).TrimStart(“https://www.codeproject.com/”);
サイト URL = サイト URL + パス;
サイトの URL を返します。
}

私が試したこと:

ブラウザ(Google Chrome)でファイル名をクリックしてファイルを開こうとしていますが、アドレスが正しいにもかかわらずファイルをクリックしてもブラウザがファイルを開きません

解決策 1

以下のコードを試してください –

C#
WebClient req = new WebClient();
HttpResponse response = HttpContext.Current.Response;
response.Clear();
response.ClearContent();
response.ClearHeaders();
response.Buffer = true;

response.AddHeader("Content-Disposition", "attachment;filename=\"" + System.IO.Path.GetFileName(HttpContext.Current.Server.MapPath(FileName)) + "\"");
byte[] data = req.DownloadData(HttpContext.Current.Server.MapPath(FileName));
response.BinaryWrite(data);
response.End();

解決策 2

ハイパーリンクを使用してブラウザでファイルを開くにはどうすればよいですか?

コメント

タイトルとURLをコピーしました