gpt4 book ai didi

javascript - 回发后如何显示成功和错误消息?

转载 作者:行者123 更新时间:2023-12-02 16:40:38 24 4
gpt4 key购买 nike

我正在使用 ASP.NET 和 C#。

我有一个表单,用户可以在其中将文件上传到服务器。它使用 Javascript 进行客户端验证检查,以确保它是正确的文件类型等。如果通过,控制权将移交给 aspx.cs 代码隐藏文件。上传完成后,我想在同一页面上显示成功通知,或者在抛出任何错误时显示错误通知。

如何创建一个可以传递回 aspx 文件并显示的变量?

上传.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>

上传.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
}
}


}
}

最佳答案

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.fileupload.saveas%28v=vs.110%29.aspx

此链接包含上传成功消息。

<asp:Label id="UploadStatusLabel"
runat="server">
</asp:Label>

如果成功使用UploadStatusLabel.Forecolor = green

关于javascript - 回发后如何显示成功和错误消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27530346/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com