文件上传与删除示例

本应用提供了一个简单的文件上传和删除功能。用户可以上传文件到服务器,并在上传完成后进行浏览和删除操作。进度条会显示每个文件的上传进度。

要使用这个应用,首先需要下载并解压上传文件到指定的目录,然后设置相应的访问权限,并在浏览器中访问指定的URL。

以下是使用步骤:

  1. 下载并解压上传文件到 C:\inetpub\wwwroot\Upload 目录。
  2. 设置 C:\inetpub\wwwroot\Upload\upload 文件夹的访问权限,允许所有人(或IIS匿名用户)访问。
  3. 在浏览器中访问 http://localhost/Upload/Upload.aspx?folder=upload
  4. 如果需要,可以通过修改URL中的 ?folder=upload 来指定不同的文件夹,例如 ?folder=folder1

以下是JavaScript代码,用于处理文件上传的逻辑:

var oUploadedFiles = []; function OnLoad() { if (_("tbServer"))_("btnDelete").style.display = ""; _("file1").addEventListener("change", FileSelectHandler, false); var xhr = new XMLHttpRequest(); if (xhr.upload) { var filedrag = _("divDropHere"); if (filedrag){ filedrag.addEventListener("dragover", FileDragHover, false); filedrag.addEventListener("dragleave", FileDragHover, false); filedrag.addEventListener("drop", FileSelectHandler, false); filedrag.style.display = "block"; } _("btnUpload").style.display = "none"; } } function FileDragHover(e) { e.stopPropagation(); e.preventDefault(); e.target.className = (e.type == "dragover")?"hover":""; } function UploadFile(file,i) { var xhr = new XMLHttpRequest(); if (xhr.upload) { var progress = _("progressBar"+i).appendChild(document.createElement("div")); progress.className = "progressBar"; progress.innerHTML = ""; xhr.upload.addEventListener("progress", function(e) { var pc = parseInt(100 - (e.loaded / e.total * 100)); progress.style.backgroundPosition = pc + "% 0"; }, false); xhr.onreadystatechange = function(e) { if (xhr.readyState == 4) { progress.className = "progressBar " + (xhr.status == 200?"progressSuccess":"progressFailed"); if (xhr.status == 200){ oUploadedFiles[oUploadedFiles.length] = {name: file.name, size: file.size}; _("btnDelete").style.display = ""; } } }; var oFormData = new FormData(); oFormData.append("myfile"+i, file); xhr.open("POST", _("form1").action, true); xhr.send(oFormData); } } Dim sFolder As String = "upload" Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Request.QueryString("folder") <> "" Then sFolder = Request.QueryString("folder") End If Dim sFolderPath As String = Server.MapPath(sFolder) If System.IO.Directory.Exists(sFolderPath) = False Then Response.Write("Folder does not exist: " & sFolderPath) Response.End() End If If Request.HttpMethod = "POST" Then If Request.Form("btnDelete") <> "" Then 'Delete files If (Not Request.Form.GetValues("chkDelete") Is Nothing) Then For i As Integer = 0 To Request.Form.GetValues("chkDelete").Length - 1 Dim sFileName As String = Request.Form.GetValues("chkDelete")(i) Try System.IO.File.Delete(sFolderPath & "\" & sFileName) Catch ex As Exception 'Ignore error End Try Next End If Else 'Upload Files For i As Integer = 0 To Request.Files.Count - 1 Dim oFile As System.Web.HttpPostedFile = Request.Files(i) Dim sFileName As String = System.IO.Path.GetFileName(oFile.FileName) oFile.SaveAs(sFolderPath & "\" & sFileName) Next End If End If End Sub
沪ICP备2024098111号-1
上海秋旦网络科技中心:上海市奉贤区金大公路8218号1幢 联系电话:17898875485