PDA

View Full Version : hoi ve download file



maitrinh
07-07-2003, 15:58
Toi muon viet 1 trang co phan download danh cho user , de khi user can download file ve may xem thi phai lam sao? co the file .doc hay .xml hay .zip. Ai biet xin chi giup toi. Rat gap. thanks.

nguyenthu
08-07-2003, 13:53
Bạn dùng thẻ <a>, thí dụ :
<a href="file_cua_ban.zip">Download file_cua_ban</a>
Thân

maitrinh
15-07-2003, 15:39
minh da lam nhu vay roi, nhung chi co the duoc voi file duoi .zip thoi. Con voi cac duoi nhu: .doc, .xls, vv... thi ko duoc. No se bi mo ra luon trong may cua user ma chua hoi xem nguoi ta co can download ko?

1011
15-07-2003, 17:08
Muốn bấm vào link là down luôn bạn phải lưu content-type, filesize, và filename của file được upload trong database nữa. Các thông tin này nên lưu trong một bảng riêng.
Sau đó viết một trang làm nhiệm vụ là download thôi (download.asp)
link trong trang cần down:


<a href=download.asp?fileid=fid>ten_file_download</a>
' fid: primary key của table chứa file download


mã của trang download.asp


Response.Buffer = True
...
'lấy các thông tin về file đã được lưu trong DB
content_type=Rs("content_type")
file_size=Rs("file_size")
file_name=Rs("file_name")

Response.AddHeader("Content-Disposition", "attachment; filename=" & file_name)
Response.AddHeader("Content-Length", CInt(file_size))
Response.ContentType = content_type

'lấy đường dẫn file download, giả sử các file upload được lưu trong thư mục FileAttachs
strFilePath = Server.MapPath("FileAttachs/" & file_name)

Set NewFile = New FileStream(strFilePath, FileMode.Open)
NewFile.Read(Buffer, 0, NewFile.Length)
Response.BinaryWrite(Buffer)
Response.Flush()
NewFile.Close()
Response.End()


Chú ý: Khi upload file bạn lưu luôn content-type, file-size và file name.

pixelart
18-07-2003, 13:56
Nếu có thể xin bạn chỉ giúp luôn về PHP được ko ? cám ơn nhiều

1011
18-07-2003, 14:44
Viết bằng PHP cũng tương tự thôi. Nghĩa là làm sao đó để trả về cho trình duyệt biết được content-type, file-size của file cần download thôi. Quan trọng nhất là phải dùng phương thức BinaryWrite() của đối tượng Response thôi.
Bạn có thể tìm code phần này trong các forum PHP như: phpBB, IBF, vBB. Chắc chắn là có. Hình như trong đĩa CD Lê Hoàn cũng có, bạn kiếm thử xem.

pixelart
18-07-2003, 21:57
Nếu có thể, xin bạn viết thử một script cho PHP giúp mình với

White_Rose
18-07-2003, 22:45
Maybe it will help u

http://genetic.vngate.net/diendan/thread.php?threadid=4380&boardid=21&styleid=4

romeo
20-07-2003, 23:49
Có lẽ trả lời giờ hơi muộn, nhưng có khi lại vẫn hữu ích:
<?php
header("Content-type: application/pdf");
header("Content-Disposition: attachment; filename=downloaded.pdf");

/* ... output pdf file ... */

?>

From PHP manual.

pixelart
21-07-2003, 12:33
oh , cám ơn bạn nhiều lắm