導航:首頁 > 文件教程 > asp下載文件代碼

asp下載文件代碼

發布時間:2023-05-27 18:08:58

① asp視頻下載的代碼

把下面的代碼保存為download.asp,然後把下載鏈接改成<a href="Download.asp?FileName=***.rm">下載視頻</a>就可以了:
<%FileName=Request.Querystring("FileName")
Function GetFileName(longname)
while instr(longname,"/")
longname=right(longname,len(longname)-1)
wend
GetFileName=longname
End Function
Const adTypeBinary=1
FileExt=Mid(FileName,InStrRev(FileName,".")+1)
Response.Clear
Response.AddHeader "content-disposition","attachment;filename="&GetFileName(FileName)
Set Stream=server.CreateObject("ADODB.Stream")
Stream.Type=adTypeBinary
Stream.Open
'這里把DownloadFiles改成下載文件所在文件夾與本頁的相對路徑
TrueFileName="DownloadFiles/"&FileName
Stream.LoadFromFile Server.MapPath(TrueFileName)
While Not Stream.EOS
Response.BinaryWrite Stream.Read(1024*64)
Wend
Stream.Close
Set Stream=Nothing
Response.Flush
Response.End%>

② 怎樣用asp實現,下載指定網址文件,並重命名後保存到本地伺服器上

download.asp
<%
'Code By oday

url =Trim(Request.QueryString("url")) '注意URL路徑上的文件不能是被IIS解析的,如.txt就不行,要用的話自己改個後綴
fname=Trim(Request.QueryString("fname"))

if url <> "" then 'and fname<>"" then
Set xPost = CreateObject("Microsoft.XMLHTTP")
xPost.Open "GET",url,False
xPost.Send()
Set sGet = CreateObject("ADODB.Stream")
sGet.Mode = 3
sGet.Type = 1
sGet.Open()
sGet.Write(xPost.responseBody)
sGet.SaveToFile Server.MapPath(".")&"/"&fname,2
set sGet = nothing
set sPOST = nothing
response.Write("下載成功!<br>")
end if

%>
test.asp
<script>
location.href="download1.asp?url="+escape("http://58.211.102.206/hi/流行音樂/青花瓷.mp3")+"&fname=demo.mp3"
</script>

現在 這個可以運行了!!原來那個 ajax 有點問題!

③ ASP.NET 中,實現download下載,彈出打開和保存對話框,不限制文件大小,跪求實現代碼,謝謝了

思路很簡單,讀取伺服器文件路徑,然後再保存數據流,下面是實現代碼:
(ps:因為要上班,來不及寫很多注釋,關鍵的地方加了幾句注釋哈)

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using YTLib.Basic;
using YTLib.YTDBC;
using System.IO;
using System.Threading;
namespace siteadmin.admin
{
public partial class downfile : System.Web.UI.Page
{

protected void Page_Load(object sender, EventArgs e)
{
string strSQL = "";

YTNDBObject dbo = new YTNDBObject();
dbo.DataName = "Main";
Page.EnableViewState = false;
if(!IsPostBack)
{
string sid=Request["id"];
if(!YTLib.publicOP.IsNumString(sid)||sid==null)
sid="0";
strSQL = "select * from FileSource where ID=@ID";
dbo.PrepareCommand(strSQL);
dbo.SetCmdIntValue("@ID",int.Parse(sid));
DataTable dt=dbo.QueryData().Tables[0];
string sourceName = (string)dt.Rows[0]["FilePath"];
sourceName.Trim();//消除空格
string filesnames = sourceName.Substring(7, sourceName.Length - 8);
int index= sourceName.LastIndexOf(".");
string extend = sourceName.Substring(index+1);//擴展名
string fullPath = "~/uploaded/" + sourceName;
fullPath = Server.MapPath(fullPath);
//讀出該資源的下載次數
int downloadtimes = 0;

downloadtimes = int.Parse(dt.Rows[0]["downloadCounts"].ToString());

Page.Response.Clear();
bool success = ResponseFile(Page.Request, Page.Response, filesnames, fullPath, 1024000);
if (!success) Response.Write("<script language=\"javascript\">alert(\"Download file error\");window.location.href=\"../Download.aspx\"</script>");
else
{
//記錄下載次數
downloadtimes++;
string sqlStr = "update FileSource set downloadCounts=" + downloadtimes + " where ID=@IID";
dbo.PrepareCommand(sqlStr);
dbo.SetCmdIntValue("@IID",int.Parse(sid));
dbo.ExecuteNoQuery();
}
Response.Write("<script language=\"javascript\">window.location.href=\"../Download.aspx\"</script>");
}
}
public static bool ResponseFile(HttpRequest _Request, HttpResponse _Response, string _fileName, string _fullPath, long _speed)
{
try
{
FileStream myFile = new FileStream(_fullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
BinaryReader br = new BinaryReader(myFile);
try
{
_Response.AddHeader("Accept-Ranges", "bytes");
_Response.Buffer = false;//不緩沖
long fileLength = myFile.Length;
long startBytes = 0;
double pack = 10240;
//10K bytes
int sleep = 200;
//每秒5次 即5*10K bytes每秒 int sleep = (int)Math.Floor(1000 * pack / _speed) + 1;
if (_Request.Headers["Range"] != null)
{
_Response.StatusCode = 206;
string[] range = _Request.Headers["Range"].Split(new char[] { '=', '-' });
startBytes = Convert.ToInt64(range[1]);
}
_Response.AddHeader("Content-Length", (fileLength - startBytes).ToString());
if (startBytes != 0)
{
//Response.AddHeader("Content-Range", string.Format(" bytes {0}-{1}/{2}", startBytes, fileLength-1, fileLength));
}
_Response.AddHeader("Connection", "Keep-Alive");
_Response.ContentType = "application/octet-stream";
_Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(_fileName, System.Text.Encoding.UTF8));
br.BaseStream.Seek(startBytes, SeekOrigin.Begin);
int maxCount = (int)Math.Floor((fileLength - startBytes) / pack) + 1;
for (int i = 0; i < maxCount; i++)
{
if (_Response.IsClientConnected)
{
_Response.BinaryWrite(br.ReadBytes(int.Parse(pack.ToString())));
Thread.Sleep(sleep);
}
else
{
i = maxCount;
}
}
}
catch
{
return false;
}
finally
{
br.Close();
myFile.Close();
}
}
catch
{
return false;
}
return true;
}

}
}

④ asp流輸出文件下載

fullpath 是指物理全路徑。

例如: E:\web\download\aaaaa.rar

把組合好了的 fullpath 傳遞給這個參數就行了。 會自動打回開答下載。

使用: call downloadfile(server.MapPath("download/aaaaa.rar"))

⑤ 如何用ASP實現文件下載

調用
response.Write("<a href=down.asp?filename="&UpLoadPath&ls_array(i+1)&">"&ls_array
(i)&"</td></tr>")

down.asp文件內容如下:
<%
Const FilePath = "UploadFile/" '文件存放路徑
From_url = Cstr(Request.ServerVariables("HTTP_REFERER"))
Serv_url = Cstr(Request.ServerVariables("SERVER_NAME"))
Function GetFileName(longname)'/folder1/folder2/file.asp=>file.asp
while instr(longname,"/")
longname = right(longname,len(longname)-1)
wend
GetFileName = longname
End Function
Dim Stream
Dim Contents
Dim FileName
Dim TrueFileName
Dim FileExt
Const adTypeBinary = 1
FileName = Request.QueryString("FileName")
if FileName = "" Then
Response.Write "無效文件名!"
Response.End
End if
FileExt = Mid(FileName, InStrRev(FileName, ".") + 1)
Response.Clear

if lcase(right(FileName,3))="gif" or lcase(right(FileName,3))="jpg" or lcase(right(FileName,3))="png" then
Response.ContentType = "image/*" '對圖像文件不出現下載對話框
else
Response.ContentType = "application/ms-download"
end if
Response.AddHeader "content-disposition", "attachment; filename=" & GetFileName(Request.QueryString("FileName"))
Set Stream = server.CreateObject("ADODB.Stream")
Stream.Type = adTypeBinary
Stream.Open
TrueFileName= FilePath &FileName

Response.Write TrueFileName
Response.End

Stream.LoadFromFile Server.MapPath(TrueFileName)
While Not Stream.EOS
Response.BinaryWrite Stream.Read(1024 * 64)
Wend
Stream.Close
Set Stream = Nothing
Response.Flush
Response.End
%>

⑥ ASP網站大文件下載的問題

<%
host=Request.ServerVariables("HTTP_HOST")
fwym="http://" & host & Request.ServerVariables("SCRIPT_NAME")

wenjian=request("wenjian")
select case true
case right(lcase(wenjian),4)=".asp"
response.write "該類型文件不允許下載。"
response.end
case right(lcase(wenjian),4)=".php"
response.write "該類型文件不允許下載。"
response.end
case right(lcase(wenjian),5)=".aspx"
response.write "該類型文件不允許下載。"
response.end
end select
wenjian=replace(wenjian,"/","\")
a=split(wenjian,"\")
filename=a(ubound(a))
Set fso = CreateObject("Scripting.FileSystemObject")
fileurl=left(fwym,len(fwym)-8) & wenjian
fileurl=replace(fileurl,"\","/")

if instr(1,wenjian,":")=0 then wenjian=server.mappath(wenjian)

'設置可下載文件大小上限
maxsize=50000000
'response.write wenjian
'response.end
set f1=fso.getfile(wenjian)
if f1.size>maxsize then
response.write "文件太大了,不允許下載。" & dxzh(f1.size) & "只支持" & dxzh(maxsize) & "以內的文件下載"
response.end
end if

if instr(1,wenjian,":")=0 then
'response.write fileurl
response.redirect fileurl
response.end
end if

Response.ContentType = "application/octet-stream"
Response.AddHeader "content-disposition", "attachment; filename =" & filename
set objstream = Server.CreateObject("adodb.stream")
objstream.Mode = 3
objstream.Type = 1
objstream.Open
objstream.LoadFromFile (wenjian)
BytesToBstr = objstream.Read

response.binarywrite BytesToBstr

function dxzh(dx)
zijieshu = dx & "位元組"
kbshu = FormatNumber(dx / 1024, "0.0") & "KB"
mbshu = FormatNumber(dx / 1024 / 1024, 2) & "MB"
fsize = dx
If fsize / 1024 < 1 Then dxzh = zijieshu
If fsize / 1024 >= 1 And fsize / 1024 < 1024 Then dxzh = kbshu
If fsize / 1024 >= 1024 Then dxzh = mbshu
end function
%>

⑦ asp.net中用文件流方式下載文件,後台代碼無錯,前台瀏覽器沒有下載反應

stringfilename=context.Request["fileName"].ToString();
stringfileName="【下載】"+filename;//客戶端保存的文件名
stringfilePath=System.Web.HttpContext.Current.Request.MapPath("~/UpFile/TraceCode/"+filename+"");//路徑
FileStreamfileStream=newFileStream(filePath,FileMode.Open);
byte[]bytes=newbyte[(int)fileStream.Length];
fileStream.Read(bytes,0,bytes.Length);
fileStream.Close();
System.Web.HttpContext.Current.Response.ContentType="application/octet-stream";
System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition","attachment;filename="+fileName);
System.Web.HttpContext.Current.Response.BinaryWrite(bytes);
System.Web.HttpContext.Current.Response.Flush();
//System.Web.HttpContext.Current.Response.End();
//System.Web.HttpContext.Current.Response.Close();
System.Web.HttpContext.Current.ApplicationInstance.CompleteRequest();

⑧ ASP 如何實現文件下載

你把要下載的文件名傳到下載頁面,用request("fileNameField")獲取文件名
下面這地方改一下

iConcStr = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False" & _
";Data Source=" & server.mappath(request("fileNameField"))

點擊回下載答的地方用<a href='下載頁面路徑?fileNameField=要下載的文件名'>下載文件</a>
這個

閱讀全文

與asp下載文件代碼相關的資料

熱點內容
windows8顯示隱藏文件 瀏覽:603
ipad2可以升級到92嗎 瀏覽:857
如何打開ps保存的文件 瀏覽:744
幼兒編程教育有哪些 瀏覽:453
汽車發外地用什麼app 瀏覽:810
網路如何贊美女人漂亮 瀏覽:143
如何把桌面文件放到excel裡面 瀏覽:363
照片文件名是怎麼查的 瀏覽:876
c怎麼在cmd模式下顯示文件 瀏覽:325
手機怎麼把文件夾的圖片移到相冊 瀏覽:440
hjc是啥文件的格式 瀏覽:298
報廢鐵皮文件櫃圖片 瀏覽:801
win10系統更新文件能 瀏覽:558
怎麼讓蘋果手機下載其他APP 瀏覽:471
多個cs文件編譯成一個dll 瀏覽:606
sql管理工具70 瀏覽:130
js裡面的圖片對齊 瀏覽:965
三星2016視頻文件夾 瀏覽:317
舊手機創新手機數據怎麼傳 瀏覽:954
怎麼刪除領克app里的記錄 瀏覽:254

友情鏈接