一段簡單 ASP檔案上傳執行個體

來源:互聯網
上載者:User

本教程將告訴你需要建立一個簡單的應用程式,使使用者能夠上傳PDF檔案到伺服器的步驟,刪除現有的檔案和查看他們的瀏覽器中的檔案。這個例子使用我們自己的dgFileUpload組成部分,你可以下載一個15天的試用版在這裡,您可以按照教程和測試自己的伺服器完成的應用程式。所有的原始碼,隨著小Access 2000資料庫教程在本教程中使用,可以下載在這裡作為一個ZIP檔案。

在下載並取消荏苒存檔,您的網站根目錄下建立(如的'C: inetpub wwwroot的 dgUpload'),並把兩個ASP頁(新目錄'upload.asp教程'和'display.asp')和在那裡的資料庫。您還必須建立一個名為'PDF的'和設定此目錄的安全保障,使為'完全控制Internet來賓帳戶'使用者。要做到這一點,右鍵點擊'PDF檔案的檔案夾,並選擇'屬性',然後'安全'標籤。如果'Internet來賓帳戶'不是已經列出,請單擊“添加'按鈕,然後選擇電腦名稱從'尋找'下拉式功能表。向下滾動,直至看到'為IUSR_ [電腦名稱]',選擇此使用者和點擊'添加'按鈕。點擊'確定'重新回到'屬性'視窗中,選擇新添加的使用者,給這個項目'完全控制'。您現在可以測試通過輸入以下網址申請入瀏覽器:

http://localhost/dgUpload/upload.asp

在這個頁面你會看到兩個輸入欄位,第一,您可以選擇您想要的PDF檔案上傳和其他讓你輸入該檔案的顯示名稱。此名稱將顯示為一個連結,查看'display.asp檔案'後上傳。瀏覽到一個合適的PDF檔案,輸入名稱,然後點擊'上傳'按鈕。您的檔案將被上傳到,你會被重新導向到顯示網頁,其中將有1到檔案的連結。有關詳情(路徑,顯示名稱和日期上傳每個檔案)儲存在資料庫中。讓我們來看看代碼執行此。

我們在聲明變數,我們首先要做的是開啟一個串連到資料庫。

Set cnnDB = Server.CreateObject("ADODB.Connection")
strConn = "DRIVER={Microsoft Access Driver (*.mdb)};" & _
               "DBQ=" & Server.MapPath("newsletters.mdb")
cnnDB.Open strConn

Set oPost = Server.CreateObject("dgFileUpload.dgUpload")

Because we want to restrict the file size of the uploaded PDFs to something reasonable we will set this to 10 Megabytes.

oPost.MaxFileSize = 10000000

Next we grab the data posted from the form by calling the 'GetPostData' method.

oPost.GetPostData()


下面的代碼抓起檔案名稱和顯示名稱從形式收集,建立並執行SQL查詢來儲存在資料庫中的檔案詳細資料,然後儲存該檔案到伺服器上指定的目錄。查看'源upload.asp'來看看這個代碼與應用程式的其餘部分整合。


 get the filename and display name
strDisplayName = oPost.Form("displayText")
strFileName = oPost.Files("pdfFile").FileName

' build the INSERT query
strQuery = "INSERT INTO NewsLetters (Filename, DisplayName) VALUES ('" & strFilename & "', '" & strDisplayName & "')"
cnnDB.Execute(strQuery)

' get the folder where we want to store the pdf
strFolder = Server.MapPath("pdfs") & ""

' save the uploaded pdf to the specified directory
intResult = oPost.Files("pdfFile").Save(strFolder & strFileName, 1)

' if the file upload is successful, redirect to
' the page that displays the files ("display.asp")
If intResult = 0 Then
    cnnDB.Close
    Set cnnDB = Nothing
    Response.Redirect("display.asp")
Else
    strFeedBack = "File upload failed. Error #" & intResult
End If


如果有問題或與上傳儲存過程中的'strFeedback'變數被分配一個合適的資訊,然後顯示給使用者。這是處理使用用戶端JavaScript一旦頁面已被處理。再次,看提供的原始碼是如何工作的更多細節。

正如前面提到的,上面的例子使用dgFileUpload但這一進程是任何類似的檔案上傳組件使用。本教程的其餘部分是提取一個檔案由邁克爾艾倫史密斯書面上傳組件的文章,描述了上傳檔案使用其他兩個組件的過程,軟體和持續存在AspUpload工匠FileUp。

兩者(FileUp和AspUpload)處理上傳略有不同。讓我們這些組件的代碼來處理我們的每一個要求(上傳僅低於指定大小的影像檔)。

的形式,在用戶端上傳一個檔案,是相同的兩個。

form name="form" action="upload.asp" enctype="MULTIPART/FORM-DATA" method="POST">
<input type="file" name="file1">
<input type="submit" value="Upload Files">
</form>

<%
intMaxFileSize = 8000
strUploadFolder = "c:uploadFolder"

Function isFileSizeOK(bytes)
' restrict file byte size
  byteMAX = intMaxFileSize
  If bytes > byteMAX Then
    isFileSizeOK = FALSE
  Else
    isFileSizeOK = TRUE
  End If
End Function

Function isValidFile(filename)
' define what file types you will permit to upload
  fileExtension = lcase(right(filename,4))
  select case fileExtension
  case ".gif",".jpg",".png","jpeg"
    isValidFile = TRUE
  case else
    isValidFile = FALSE
  end select
End Function
%>

Now the code for Software Artisans FileUp.

<%
Sub uploadSA
Set up = Server.CreateObject("SoftArtisans.FileUp")
up.Path = uploadFolder
If NOT up.IsEmpty Then
  filename = Mid(up.UserFilename, InstrRev(up.UserFilename, "") + 1)
' restrict file types to upload
  If isValidFile(filename) Then
' restrict file by size
  If isFileSizeOK(up.TotalBytes) Then
    up.Save
    strUploadStatus1 = "File [" & filename & "] Uploaded Successfully! " &     up.TotalBytes
  Else
    strUploadStatus1 = "ERROR: File Too Large: " & filename & " (" &     up.TotalBytes & " bytes)"
  End If
Else
  strUploadStatus1 = "ERROR: This File Type is restricted from uploading: "   & filename
  End If
End If
Set up = Nothing
End Sub
%>

堅持的AspUpload,您儲存檔案的第一個,然後執行檢查。如果該檔案沒有通過檢查,那麼代碼從伺服器中刪除它。該組件還具有“SaveToMemory”選項,它繞過寫入到磁碟,直到指示。 ASPUpload具有內建影像處理和大小可以檢測一個檔案是一個帶有。ImageType物業的形象,但這個例子中,我們將使用isValidFile功能。

<%
Sub uploadPersists
Set up = Server.CreateObject("Persits.Upload.1")
up.OverwriteFiles = TRUE
up.SetMaxSize intMaxFileSize
up.Save uploadFolder
For Each File in up.Files
  fileName = File.ExtractFileName
  If isValidFile(fileName) Then
    If isFileSizeOK(File.OriginalSize) Then
    strUploadStatus2 = "File [" & filename & "] Uploaded Successfully! "
  Else
    strUploadStatus2 = "ERROR: File Too Large: " & fileName & " (" &     File.OriginalSize & " bytes)"
    File.Delete
  End If
Else
  File.Delete
  strUploadStatus2 = "ERROR: This File Type is restricted from uploading: "   & fileName
End If
Next
End Sub
%>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.