【发布时间】:2020-02-04 01:51:51
【问题描述】:
我打算做的是当我点击上传按钮时,系统会检查选择的文件名是否与我想要的文件名相同(在文件上传之前)。
string selectedValue = version.SelectedItem.Value;
string serverPath;
if (FileUpload1.PostedFile.FileName != "")
{
if (selectedValue == "1")
{
// check extension of file before uploading
if (FileUpload1.PostedFile.FileName == "MyApp.apk")
{
serverPath = "C:/MyPath/MyApp.apk";
FileUpload1.PostedFile.SaveAs(serverPath);
ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('file has been uploaded successfully');window.location.href = '" + Request.RawUrl + "'; ", true);
}
else if (FileUpload1.PostedFile.FileName == "MyApp.ipa")
{
serverPath = "C:/MyPath/MyApp.ipa";
FileUpload1.PostedFile.SaveAs(serverPath);
ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('file has been uploaded successfully');window.location.href = '" + Request.RawUrl + "'; ", true);
}
else
{
ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('Please select MyApp file to upload');window.location.href = '" + Request.RawUrl + "'; ", true);
}
}
}
但我得到的是,系统会先上传文件并显示我设置的警报消息。
如何在默认上传栏出现之前先显示错误消息(选择了错误的文件)?
【问题讨论】:
-
comapre 字符串。
if (FileUpload1.PostedFile.FileName.Equals(selectedFileName)) { // proceed with upload }
标签: c# asp.net file-upload