여러 이미지를 서버에 업로드하는 다음 코드가 있습니다. 선택한 모든 이미지가 크기 제한 및 올바른 파일 확장자보다 작 으면 잘 작동합니다. 그러나 하나의 이미지 만 해당 기준을 충족하지 못하면 다른 모든 이미지는 게시되지 않습니다. 왜? 기준을 충족하지 못하는 이미지 만 실패하고 싶습니다.
protected void uploadFile_Click(object sender, EventArgs e)
{
if (UploadImages.HasFiles)
{
string strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
SqlConnection con = new SqlConnection(strConnString);
con.Open();
try
{
foreach (HttpPostedFile uploadedFile in this.UploadImages.PostedFiles)
{
string FileExtension = System.IO.Path.GetExtension(UploadImages.FileName);
if (FileExtension == ".jpg" || FileExtension == ".jpeg" || FileExtension == ".gif" || FileExtension == ".tiff")
{
UploadStatusLabel.Text = "";
int fileSize = UploadImages.PostedFile.ContentLength;
if (fileSize < 150000)
{
UploadStatusLabel.Text = "";
string newname = System.DateTime.Now.ToString("yyMMdd-hhmmss-") + uploadedFile.FileName;
uploadedFile.SaveAs(System.IO.Path.Combine(Server.MapPath("/Images/Editors/BG/"), newname));
listofuploadedfiles.Text += string.Format("<br />Uploaded successfuly <img width='100px' src='/Images/Editors/BG/{0}'/>{0}<br clear='all'/>", newname );
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = @"INSERT INTO BackgroundImages(BG_fileName, IDuser) VALUES(@param1,@param2)";
cmd.Parameters.AddWithValue("@param1", newname);
cmd.Parameters.AddWithValue("@param2", HttpContext.Current.User.Identity.GetUserId());
cmd.ExecuteNonQuery();
}
else
{
UploadStatusLabel.Text = "<strong>" + uploadedFile.FileName + "</strong> NOT uploaded. Size exceeds 150 kb limit. ";
}
}
else
{
UploadStatusLabel.Text = "<strong>" + uploadedFile.FileName + "</strong> NOT uploaded. Wrong image format. Only jpg, jpeg, gif and tiff images are accepted. ";
}
}
}
catch (Exception ex)
{
Response.Write("Error while inserting record on table..." + ex.Message + "Insert Records");
}
finally
{
con.Close();
con.Dispose();
}
}
else
{
UploadStatusLabel.Text = "You Haven't selected a file to upload.";
}
}
루프 내의 특정 항목 / 파일에서만 작업하십시오.
따라서 다음과 같습니다. string FileExtension = System.IO.Path.GetExtension (UploadImages.FileName); 다음과 같아야합니다. string FileExtension = System.IO.Path.GetExtension (uploadedFile.FileName);
그리고 이것은 : int fileSize = UploadImages.PostedFile.ContentLength; 다음과 같아야합니다. int fileSize = uploadFile.PostedFile.ContentLength;
현재 루프에있는 특정 파일 인 uploadFile 대신 UploadImages에서 fileSize를 가져옵니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다