iT邦幫忙

1

asp 下載word 亂碼

asp

大家好:

想請問用asp下載word會產生亂碼..pdf或用zip打包則不會,想問有甚麼方法
https://ithelp.ithome.com.tw/upload/images/20210407/20097057nUO5asjhB6.png
謝謝

<%
Set fs=Server.CreateObject("Scripting.FileSystemObject")
fName="porojecpaper.doc"
sDir= "./pap/" & fName
tDir= "./Down/" & fName
sourceDir=Server.MapPath(sDir)
backupDir=Server.MapPath(tDir)
fs.CopyFile sourceDir,backupDir
if fs.FileExists(backupDir)  then 
Const adTypeBinary = 1 
Set objStream = Server.CreateObject("ADODB.Stream") 
objStream.Open
objStream.Charset = "utf-8"  
objStream.Type = adTypeBinary 
objStream.LoadFromFile backupDir
Response.AddHeader "Content-Disposition", "attachment; filename=" & fName 
Response.AddHeader "Content-Length", strFileSize 
Response.Charset = "utf-8" 
Response.ContentType = "application/msword" 
Response.BinaryWrite objStream.Read 
Response.Flush 
objStream.Close 
Set objStream = Nothing
fs.DeleteFile backupDir,True

else
response.write "檔案不存在,請洽承辦人"
end if
%>

你的前端頁面是BIG5還是UTF-8格式?
mayyola iT邦新手 1 級 ‧ 2021-04-07 14:57:36 檢舉
連結這網頁的頁面是big5
這下載的網頁是utf-8
不過我通通改成big5也會亂碼
docx可以,但進入word還要點按鈕才能進去

2 個回答

1
純真的人
iT邦高手 1 級 ‧ 2021-04-07 14:55:35
最佳解答

給他轉址下載@@..

<%
Set fs=Server.CreateObject("Scripting.FileSystemObject")
fName="porojecpaper.doc"
sDir= "./pap/" & fName
sourceDir=Server.MapPath(sDir)
if fs.FileExists(sourceDir)  then 
Response.Redirect sDir
else
response.write "檔案不存在,請洽承辦人"
end if
%>
看更多先前的回應...收起先前的回應...
mayyola iT邦新手 1 級 ‧ 2021-04-07 14:59:01 檢舉

嗯..這好像可以,不過轉址後可以fs.DeleteFile backupDir,True 嗎@@?

那行刪除~因為那句是原先複製檔案~再把複製檔案刪除的~

mayyola iT邦新手 1 級 ‧ 2021-04-07 15:14:57 檢舉

可以下載,不過複製過的檔案無法刪除@@

恩??可是你原先是@@~原始檔案複製→再把複製檔案下載@@...
我改這樣是直接下載原始檔案了@@~所以沒刪除~

mayyola iT邦新手 1 級 ‧ 2021-04-08 12:11:45 檢舉

謝謝
剛找到資料先把原本的資料夾中的檔案刪除再copy
function delfile(dDir)
Set fs=Server.CreateObject("Scripting.FileSystemObject")
backupDir=Server.MapPath(dDir)
Set objFolder = fs.GetFolder(backupDir)
Set objFiles = objFolder.Files
dim curFile
For each curFile in objFiles
fs.DeleteFile(curFile)
Next
end function

0
eric_hsu58
iT邦新手 5 級 ‧ 2021-04-07 17:09:11

Response.Clear
Response.ContentType = "application/octet-stream"
Response.AddHeader "content-disposition", "attachment; filename=" & SaveName
Set Stream = server.CreateObject("ADODB.Stream")
Stream.Type = adTypeBinary
Stream.Open
Stream.LoadFromFile Server.MapPath(FileName)
While Not Stream.EOS
Response.BinaryWrite Stream.Read(1024 * 64)
Wend
Stream.Close
Set Stream = Nothing
Response.Flush
Response.End

我要發表回答

立即登入回答