簡體   English   中英

在ASP.NET中檢索上傳的文件

[英]Retrieve uploaded file in ASP.NET

我正在使用ASP.NET和C#。

我做了一個文件上傳頁面,在其中可以使用其文件上傳。 我在數據庫中保存了三個字段,

  1. 文件名稱[NVarchar]
  2. 文件[圖像]
  3. DocumentType [NVarchar]

現在,我能夠成功地在數據庫中添加記錄。 現在,我想在gridview中顯示它,例如DocumentName,DocumentType以及下載文件的鏈接。

我嘗試通過檢索記錄並將它們分配給gridview來進行嘗試,但是我僅得到兩列。

您必須創建一個提供實際文件的下載處理程序。

處理程序可以從表中檢索Binary並將其直接寫入輸出流。

然后,網格視圖將指向此處理程序

由於將文件存儲在DB中,因此必須編寫代碼以讀取文件數據,添加適當的標頭並執行response.write

例如

在您的LinkBut​​ton點擊處理程序上,代碼將類似於

private void lnkDownload_Click(object sender,args)
{

//if you are using dataset the data will be of type byte array byte[]
//assuming you have assign the binary data to byte array

byte[] data;
Response.Clear(); //clear buffer
Response.ContentType = "image/gif"; //should be the MIME type of the document see http://www.w3schools.com/media/media_mimeref.asp for the complete list
Response.AddHeader("content-disposition", "attachment;filename=" + yourfilename);  //tell the browser the file is to be downloaded
Response.BinaryWrite(data);
Response.End();

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM