① asp.net中如何寫下載代碼
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DownloadClass dd = new DownloadClass();
dd.StrUrl = "http://127.0.0.1/UI.rar";
dd.StrFileName="C:\\1.rar";
dd.DownloadFile();
Response.Write(dd.strError);
}
//測試用線程1斷點續傳下載網路上的文件到本地電腦
public class DownloadClass
{
public string StrUrl;//文件下載網址
public string StrFileName;//下載文件保存地址
public string strError;//返回結果
public long lStartPos = 0; //返回上次下載位元組
public long lCurrentPos = 0;//返回當前下載位元組
public long lDownloadFile;//返回當前下載文件長度
public void DownloadFile()
{
System.IO.FileStream fs;
if (System.IO.File.Exists(StrFileName))
{
fs = System.IO.File.OpenWrite(StrFileName);
lStartPos = fs.Length;
fs.Seek(lStartPos, System.IO.SeekOrigin.Current);
//移動文件流中的當前指針
}
else
{
fs = new System.IO.FileStream(StrFileName, System.IO.FileMode.Create);
lStartPos = 0;
}
//打開網路連接
try
{
System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(StrUrl);
long length = request.GetResponse().ContentLength;
lDownloadFile = length;
if (lStartPos > 0)
request.AddRange((int)lStartPos); //設置Range值
//向伺服器請求,獲得伺服器回應數據流
System.IO.Stream ns = request.GetResponse().GetResponseStream();
byte[] nbytes = new byte[512];
int nReadSize = 0;
nReadSize = ns.Read(nbytes, 0, 512);
while (nReadSize > 0)
{
fs.Write(nbytes, 0, nReadSize);
nReadSize = ns.Read(nbytes, 0, 512);
lCurrentPos = fs.Length;
}
fs.Close();
ns.Close();
strError = "下載完成";
}
catch (Exception ex)
{
fs.Close();
strError = "下載過程中出現錯誤:" + ex.ToString();
}
}
}
}
② 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實現文件下載
調用
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.net中實現點擊下載按鈕進行文件下載
可以直接使用a標簽的啊, href跟你文件所在的位置
⑤ asp.net 如何實現將伺服器上的文件下載到本地
給你提供一點代碼:
string fileURL = this.Server.MapPath("你要下載的文件路徑");//文件路徑,可用相對路徑
FileInfo fileInfo = new FileInfo(fileURL);
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=" +
Server.UrlEncode(fileInfo.Name.ToString()));//文件名
Response.AddHeader("content-length", fileInfo.Length.ToString());//文件大小
Response.ContentType = "application/octet-stream";
Response.ContentEncoding = System.Text.Encoding.Default;
Response.WriteFile(fileURL);
⑥ asp如何實現文件下載功能
CALL Response.Clear()//清除已經輸出的內容
CALL Server.Transfer("aaa.exe")//重定向到文件
CALL Response.End()//結束其餘代碼執行