CÁC GIẢI PHÁP LẬP TRÌNH C# - Trang 251

251

Chương 7: ASP.NET và Web Form

private void cmdUpload_Click(object sender, System.EventArgs e) {

if (FileInput.PostedFile.FileName == "") {

// Không file nào được chỉ định.

lblInfo.Text = "No file specified.";

} else {

try {

if (FileInput.PostedFile.ContentLength > 1048576) {

// Cấm các file lớn hơn 1 megabyte.

lblInfo.Text = "File is too large.";

}else {

// File được lưu vẫn giữ lại tên file gốc của nó.

string fileName =

Path.GetFileName(FileInput.PostedFile.FileName);

// Tiến trình ASP.NET phải có quyền đối với

// vị trí nó thực hiện lưu file, nếu không

// ngoại lệ "access denied" sẽ xảy ra.

FileInput.PostedFile.SaveAs(fileName);

lblInfo.Text = "File " + fileName + " uploaded.";

}

}catch (Exception err) {

lblInfo.Text = err.Message;

}

}

}

}