我正在使用ASP.NET和C#。
我有一个表单,用户可以在其中将文件上传到服务器。它使用Javascript进行客户端验证检查以确保它是正确的文件类型等。如果它通过了,则将控制传递给aspx.cs
代码隐藏文件。上传发生后,我想在同一页面上显示成功通知,或者如果出现任何错误,则显示错误通知。
如何创建可以传递回aspx
文件并显示的变量?
upload.aspx
<html>
<%@ Page Language="C#" CodeFile="upload.aspx.cs" Inherits="Upload" %>
...
<head>
<script language="Javascript">
function validate()
{
var filter = /<stuff>/;
var file1 = document.getElementById("uploadfile1").value;
var file2 = document.getElementById("uploadfile2").value;
var file3 = document.getElementById("uploadfile3").value;
//validation code to make sure uploaded file is legit
if (success)
{
return true;
}
else
{
alert("File not legit. Please correct.");
return false;
}
}
</script>
</head>
<body>
//I want to put something here like:
//<% if success is given, display "Successful Upload" in green %>
//<% if failure is given, display "Error Uploading" in red %>
<form method="post" runat="server" action="upload.aspx" name="upload" enctype="multipart/form-data">
<asp:FileUpload ID="uploadfile1" runat="server" />
<asp:FileUpload ID="uploadfile2" runat="server" />
<asp:FileUpload ID="uploadfile3" runat="server" />
<asp:Button ID="btnUpload" runat="server" Text="Upload" onClientClick="return validate()" onClick="btnUpload_Click" />
</form>
</body>
</html>
upload.aspx.cs
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Upload_File : System.Web.UI.Page
{
protected void btnUpload_Click(object sender, EventArgs e)
{
FileUpload[] files = new FileUpload[3];
files[0] = uploadfile1;
files[1] = uploadfile2;
files[2] = uploadfile3;
string rootpath = "C:\\path\\to\\directory\\";
for(int i = 0; i < 3; i++)
{
if(files[i].HasFile)
{
files[i].SaveAs(rootpath + files[i].FileName);
//I want to put something here like:
//var uploadTime = getTimeStamp();
//var VariableToPass = "Files uploaded at " + uploadTime;
//also, a way to catch error thrown and set success/error boolean
}
}
}
}
答案 0 :(得分:2)
此链接包含上传成功消息。
<asp:Label id="UploadStatusLabel"
runat="server">
</asp:Label>
如果成功使用UploadStatusLabel.Forecolor = green