Kiểm tra dung lượng File khi upload:
Kiểm tra định dạng tập tin khi upload file lên
// Get the size in bytes of the file to upload. int fileSize = FileUpload1.PostedFile.ContentLength; // Allow only files less than 2,100,000 bytes (approximately 2 MB) to be uploaded. if (fileSize < 2100000) //Xử lý
Kiểm tra định dạng tập tin khi upload file lên
if (FileUpload1.HasFile) { // Get the name of the file to upload. string fileName = Server.HtmlEncode(FileUpload1.FileName); // Get the extension of the uploaded file. string extension = System.IO.Path.GetExtension(fileName); // Allow only files with .doc or .xls extensions // to be uploaded. if ((extension == ".doc") || (extension == ".xls")) { // Append the name of the file to upload to the path. savePath += fileName; // Call the SaveAs method to save the // uploaded file to the specified path. // This example does not perform all // the necessary error checking. // If a file with the same name // already exists in the specified path, // the uploaded file overwrites it. FileUpload1.SaveAs(savePath); // Notify the user that their file was successfully uploaded. UploadStatusLabel.Text = "Your file was uploaded successfully."; } else { // Notify the user why their file was not uploaded. UploadStatusLabel.Text = "Your file was not uploaded because " + "it does not have a .doc or .xls extension."; }
Kiểm tra file đã tồn tại hay chưa: System.IO.File.Exists(pathToCheck)
Trả lờiXóaLưu ý: savePath là đường dẫn vật lý, trước khi trả về nên lưu đường dẫn ảo trên Server.
Trả lờiXóa