如何设置上传文件为空?

本文关键字:文件 何设置 设置 | 更新日期: 2023-09-27 18:15:48

我有3个文件上传,只允许用户选择图像。问题是,无论用户是否选择带有文件上传的图片,我都会将默认的"Images/noImage.jpg"路径保存到数据库中。我也相信我明白其中的原因。我将filename变量预设为""或一个空字符串。这就是我感到困惑的地方,如果我将filename变量设置为文件上传的值,我会得到一个"对象未设置为对象的实例"错误,告诉我它在寻找一个不存在的值。下面是我的尝试:

        //ImageAlt labels current Text is set to 'Images/'
        string ImgPath1 = lblImageAlt1.Text.ToString();
        string ImgPath2 = lblImageAlt2.Text.ToString();
        string ImgPath3 = lblImageAlt3.Text.ToString();   
        //Get Filename from fileupload control
        string filename1 = ""; 
        string filename2 = ""; 
        string filename3 = ""; 
        if (fileuploadimages1.HasFile)
        {
            filename1 = Path.GetFileName(fileuploadimages1.PostedFile.FileName);
            fileuploadimages1.SaveAs(Server.MapPath(ImgPath1 + filename1));                 
        }
        else
        {
            filename1 = "noImage.jpg";
        }
        if (fileuploadimages2.HasFile)
        {
            filename2 = Path.GetFileName(fileuploadimages2.PostedFile.FileName);
            fileuploadimages2.SaveAs(Server.MapPath(ImgPath2 + filename2));                
        }
        else
        {
            filename2 = "noImage.jpg";
        }
        if (fileuploadimages3.HasFile)
        {
            filename3 = Path.GetFileName(fileuploadimages3.PostedFile.FileName);
            fileuploadimages3.SaveAs(Server.MapPath(ImgPath3 + filename3));
        }
        else
        {
            filename3 = "noImage.jpg";
        }
        //Save images into Images folder
        fileuploadimages1.SaveAs(Server.MapPath(ImgPath1 + filename1));
        fileuploadimages2.SaveAs(Server.MapPath(ImgPath2 + filename2));
        fileuploadimages3.SaveAs(Server.MapPath(ImgPath3 + filename3));
        //Getting SiteSqlServer2 from web.config connectionstring
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["sqlconnection1"].ToString());
        //Open the database connection
        conn.Open();
        //Query to insert images path and name into database
        SqlCommand cmd = new SqlCommand("Insert into my_db(CreateDate,ItemName,ItemType,ImageSrc1,ImageAlt1,ImageSrc2,ImageAlt2,ImageSrc3,ImageAlt3,ItemDescription,ItemPrice,FName,LName,ContactNum,AltContactNum,IsApproved,IsExpired,Email,IsRejected) values(@CreateDate, @ItemName, @ItemType, @ImageSrc1, @ImageAlt1, @ImageSrc2, @ImageAlt2, @ImageSrc3, @ImageAlt3, @ItemDescription, @ItemPrice, @FName, @LName, @ContactNum, @AltContactNum, @IsApproved, @IsExpired, @Email, @IsRejected)", conn);
        //Passing parameters to query
        cmd.Parameters.AddWithValue("@CreateDate", CreateDate);
        cmd.Parameters.AddWithValue("@ItemName", ItemName);
        cmd.Parameters.AddWithValue("@ItemType", ItemType);
        cmd.Parameters.AddWithValue("@ImageSrc1", filename1);
        cmd.Parameters.AddWithValue("@ImageAlt1", ImgPath1 + filename1);
        cmd.Parameters.AddWithValue("@ImageSrc2", filename2);
        cmd.Parameters.AddWithValue("@ImageAlt2", ImgPath2 + filename2);
        cmd.Parameters.AddWithValue("@ImageSrc3", filename3);
        cmd.Parameters.AddWithValue("@ImageAlt3", ImgPath3 + filename3);
        cmd.Parameters.AddWithValue("@ItemDescription", ItemDescription);
        cmd.Parameters.AddWithValue("@ItemPrice", ItemPrice);
        cmd.Parameters.AddWithValue("@FName", FName);
        cmd.Parameters.AddWithValue("@LName", LName);
        cmd.Parameters.AddWithValue("@ContactNum", ContactNum);
        cmd.Parameters.AddWithValue("@AltContactNum", AltNum);
        cmd.Parameters.AddWithValue("@IsApproved", isApproved);
        cmd.Parameters.AddWithValue("@IsExpired", isExpired);
        cmd.Parameters.AddWithValue("@Email", Email);
        cmd.Parameters.AddWithValue("@IsRejected", isRejected);
        cmd.ExecuteNonQuery();
        //Close
        conn.Close();
        Response.Redirect("form.aspx");
    }

如前所述,如果映像路径设置为filename1 = Path.GetFileName(fileuploadimages1.PostedFile.FileName);....,则图像将正确上传,但是我应该能够为不希望添加所有3个或仅1个或2个或根本不添加的用户提供例外。

如何设置上传文件为空?

您可能需要检查FileUpload组件是否有文件,使用fileuploadimages1.HasFile

此外,如果您试图这样做,您不能预置FileUpload组件与预选文件?