以下是一个使用ASP.NET WebForms实现文件上传带有进度条的示例代码:
ASPX页面:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Upload.aspx.cs" Inherits="YourNamespace.Upload" %>
File Upload with Progress Bar
File Upload with Progress Bar
CodeBehind文件:
using System;
using System.IO;
using System.Web;
namespace YourNamespace
{
public partial class Upload : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
HttpPostedFile file = Request.Files["file"];
if (file != null && file.ContentLength > 0)
{
string fileName = Path.GetFileName(file.FileName);
string filePath = Server.MapPath("~/Uploads/") + fileName;
file.SaveAs(filePath);
// 可在此处执行其他自定义逻辑
Response.Write("File uploaded successfully!");
Response.End();
}
}
}
}
}
请注意,示例代码中使用了jQuery和jquery.form插件来处理文件上传和显示进度条。您需要在ASPX页面中引用这两个库。另外,示例中将文件保存在服务器的"~/Uploads/"文件夹中,请确保该文件夹存在并具有适当的写入权限。