0

我有 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 个或根本不添加的用户设置例外。

4

1 回答 1

0

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

FileUpload此外,如果您尝试这样做,您不能使用预选文件预设组件?

于 2013-05-28T16:09:01.190 回答