0
我對.NET相當陌生,剛開始學習.NET技術。我寫了一個代碼來上傳文件。代碼如下無法在asp.net上傳空白文件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication2
{
public partial class FileUpload : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
string fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName);
if (fileExtension.ToLower() != ".doc" && fileExtension.ToLower() != ".docx")
{
Label1.Text = "Only Doc and Docx files are allowed";
Label1.ForeColor = System.Drawing.Color.Red;
}
else
{
if (FileUpload1.PostedFile.ContentLength > 2097152)
{
Label1.Text = "Limit of file size is 2MB";
Label1.ForeColor = System.Drawing.Color.Red;
}
else
{
if (System.IO.File.Exists(Server.MapPath("~/Files/" + FileUpload1.FileName)))
{
Label1.Text = "This is file already exists.";
Label1.ForeColor = System.Drawing.Color.Red;
}
else
{
FileUpload1.SaveAs(Server.MapPath("~/Files/" + FileUpload1.FileName));
Label1.Text = "File uploaded successfully";
Label1.ForeColor = System.Drawing.Color.Green;
}
}
}
}
else
{
Label1.Text = "Please select a file to be uploaded";
Label1.ForeColor = System.Drawing.Color.Red;
}
}
}
}
所有的場景工作正常。但是當我試圖上傳一個空白文件時,它顯示「請選擇要上傳的文件」消息,就像我沒有選擇任何東西。有沒有辦法上傳空白文件?
你是什麼意思的空白文件,意味着你上傳一個文件,沒有內容? – Webruster
是的,文件沒有內容。 –
如果FileUpload控件沒有文件,則問題可能出現在客戶端代碼中 –