Tôi có chức năng tải tệp lên nơi người dùng có thể tải lên tệp. Tôi muốn hạn chế người dùng tải lên một số loại tệp nhất định. Các loại được phép là: .doc, .xlsx, .txt, .jpeg.Làm thế nào để hạn chế các loại tệp trong FileUpload trong MVC3?
Tôi có thể làm như thế nào?
Đây là mã tập tin tải lên thực tế của tôi:
public ActionResult UploadFile(string AttachmentName, BugModel model)
{
BugModel bug = null;
if (Session["CaptureData"] == null)
{
bug = model;
}
else
{
bug = (BugModel)Session["CaptureData"];
}
foreach (string inputTagName in Request.Files)
{
HttpPostedFileBase file1 = Request.Files[inputTagName];
if (file1.ContentLength > 0)
{
string path = "/Content/UploadedFiles/" + Path.GetFileName(file1.FileName);
string savedFileName = Path.Combine(Server.MapPath("~" + path));
file1.SaveAs(savedFileName);
BugAttachment attachment = new BugAttachment();
attachment.FileName = "~" + path.ToString();
attachment.AttachmentName = AttachmentName;
attachment.AttachmentUrl = attachment.FileName;
bug.ListFile.Add(attachment);
model = bug;
Session["CaptureData"] = model;
}
}
ModelState.Clear();
return View("LoadBug", bug);
}
@Darrin Dimitrov .... làm thế nào tôi sẽ kiểm tra phần mở rộng bạn có thể cung cấp cho tôi bất kỳ mẫu – SoftwareNerd
@Darrin Dimitrov .. .thanks xong và tôi đang hiển thị các tệp đã tải lên này trong bảng trước khi lưu Cơ sở dữ liệu ... nếu người dùng muốn xóa một trong các tệp đã tải lên, tôi có thể làm như thế nào ... – SoftwareNerd