如何预设空文件上传?

时间:2013-05-28 15:53:31

标签: c# asp.net

我有3个文件上传,只允许用户选择图像。探测是,无论用户是否选择带有文件上传的图片,我都会将默认的“Images / noImage.jpg”路径保存到我的数据库中。我也相信我理解为什么。我将filename变量预设为""或空字符串。这是我感到困惑的地方,如果我将filename变量设置为fileupload的值,我得到一个“未设置为对象实例的对象”错误,告诉我它正在查找不存在的值。这是我试过的:

        //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或根本不添加任何3的用户设置例外。

1 个答案:

答案 0 :(得分:0)

您可能需要使用FileUpload

检查fileuploadimages1.HasFile组件是否包含文件

此外,如果您尝试这样做,则无法使用预先选择的文件预设FileUpload组件?