【问题标题】:How to control submitting form if fileupload contains no file?如果 fileupload 不包含文件,如何控制提交表单?
【发布时间】:2016-02-21 00:13:14
【问题描述】:

大家好,我想请教一下如果fileupload中没有文件,如何控制提交表单。我上传文件的代码是这样的。

FileInfo fi = new FileInfo(FileUpload1.FileName);
byte[] documentContent = FileUpload1.FileBytes;

string name = fi.Name;
string extn = fi.Extension;

da.InsertCommand.Parameters.Add("@DocumentName", SqlDbType.VarChar).Value = name;
da.InsertCommand.Parameters.Add("@DocumentContent", SqlDbType.VarBinary).Value = documentContent;
da.InsertCommand.Parameters.Add("@DocumentExt", SqlDbType.VarChar).Value = extn;

cs.Open();
da.InsertCommand.ExecuteNonQuery();
cs.Close();

如果没有选择文件并且单击了提交按钮,我想要一些限制它存储在数据库中的东西。我不知道该在 if-else 语句中添加什么。请帮帮我。

【问题讨论】:

  • 还要考虑一个方面:可能有人填充选择文件,但文件是空的。所以还要检查文件长度。 FileUpload1.Content.Length > 0

标签: c# asp.net sql-server button file-upload


【解决方案1】:

您可以直接检查您的文件名是否包含任何数据。

喜欢:

 if (FileUpload1.FileName != "")
 {
FileInfo fi = new FileInfo(FileUpload1.FileName);
byte[] documentContent = FileUpload1.FileBytes;

string name = fi.Name;
string extn = fi.Extension;

// Add Connection string here //

da.InsertCommand.Parameters.Add("@DocumentName", SqlDbType.VarChar).Value = name;
da.InsertCommand.Parameters.Add("@DocumentContent", SqlDbType.VarBinary).Value = documentContent;
da.InsertCommand.Parameters.Add("@DocumentExt", SqlDbType.VarChar).Value = extn;

cs.Open();
da.InsertCommand.ExecuteNonQuery();
cs.Close();
}
else
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "popup", "alert('No Files Uploaded');", true);
}

更新:我认为你的代码有问题。你用过

  da.InsertCommand.Parameters.Add("@DocumentName", SqlDbType.VarChar).Value = name;
  da.InsertCommand.Parameters.Add("@DocumentContent", SqlDbType.VarBinary).Value = documentContent;
  da.InsertCommand.Parameters.Add("@DocumentExt", SqlDbType.VarChar).Value = extn;

插入数据。所以试试下面的方法插入数据

if (FileUpload1.FileName != "")
 {
 FileInfo fi = new FileInfo(FileUpload1.FileName);
 byte[] documentContent = FileUpload1.FileBytes;

 string name = fi.Name;
 string extn = fi.Extension;

 SqlConnection connections = new SqlConnection(strConn);
 SqlCommand command = new SqlCommand("INSERT INTO tablename(DocumentName, DocumentContent, DocumentExt) VALUES (@DocumentName, @DocumentContent, @DocumentExt)", connections);

 SqlParameter param0 = new SqlParameter("@DocumentName", SqlDbType.VarChar);
              param0.Value = name;
              command.Parameters.Add(param0);

 SqlParameter param1 = new SqlParameter("@DocumentContent", SqlDbType.VarBinary);
              param1.Value = documentContent;
              command.Parameters.Add(param1);

 SqlParameter param2 = new SqlParameter("@DocumentExt", SqlDbType.VarChar);
              param2.Value = extn;
              command.Parameters.Add(param2);
connections.Open();
int numRowsAffected = command.ExecuteNonQuery();
connections.Close();

   if (numRowsAffected != 0)
     {
     Response.Write("<BR>Rows Inserted successfully");
     }
   else
     {
    Response.Write("<BR>An error occurred uploading the image");
     }
  }
else
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "popup", "alert('No Files Uploaded');", true);
}

【讨论】:

  • 错误:必须声明标量变量“@DocumentName”。在 da.InsertCommand.ExecuteNonQuery();
  • 我认为你在上面调用连接字符串 if (FileUpload1.FileName != "") { }
  • 该错误是因为,您的 sql 命令插入查询在 {} 之外。因此,您必须在 {} 中调用所有连接和命令字符串。它类似于 if (FileUpload1.FileName != "") { //所有你的代码,包括连接字符串、sql 命令和 da 声明 }
【解决方案2】:

在模型中的属性上添加 [Required] 标签。您可以使用验证添加响应和其他内容。

【讨论】:

  • 您的提示仅适用于 MVC 框架上的项目。对于网络表单,它会有所不同。下次也请考虑这方面
【解决方案3】:

我相信这会为你解决问题

string name = fi.Name;
string extn = fi.Extension;
if(!String.IsNullOrEmpty(name))
{
    da.InsertCommand.Parameters.Add("@DocumentName", SqlDbType.VarChar).Value = name;
    da.InsertCommand.Parameters.Add("@DocumentContent", SqlDbType.VarBinary).Value = documentContent;
    da.InsertCommand.Parameters.Add("@DocumentExt", SqlDbType.VarChar).Value = extn;

    cs.Open();
    da.InsertCommand.ExecuteNonQuery();
    cs.Close();
}

所以当我们检查String.IsNullOrEmpty(name) 方法时。它检查文件名是否为空白。如果没有选择文件且值为 none 则不会调用 SQL 事物。

【讨论】:

  • 错误:路径不合法。显示在 FileInfo fi = new FileInfo(FileUpload1.FileName);
【解决方案4】:

我不确定您需要哪一个:

1. 限制提交(postback),添加RequiredFieldValidator

<asp:RequiredFieldValidator ID="Validation1" runat="server" ControlToValidate="FileUpload1" ErrorMessage="An image file is required"></asp:RequiredFieldValidator>

2。不要只保存图片:

`if(FileUpload1 != null && FileUpload1.FileName!= "")`

希望对您有所帮助...

【讨论】:

    【解决方案5】:

    使用以下场景:

      ListBox lbAttachments = (ListBox)FileUpload1;
      if(lbAttachments.Items.Count > 0)
      {
             for(int i = 0; i < lstAttachments.Items.Count; i++)
             {
                   FileInfo fi = new FileInfo(lbAttachments.Items[i].Text);
                   byte[] documentContent = FileUpload1.FileBytes;
    
                   string name = fi.Name;
                   string extn = fi.Extension;
                   SqlCommand cmdInsert = new SqlCommand("Insert into tableName(DocumentName,DocumentContent, DocumentExt) Values(@DocumentName,@DocumentContent,@DocumentExt)", con);
                   SqlParameter Name = cmdInsert.Parameters.Add("@DocumentName", SqlDbType.VarChar, 100);
                   SqlParameter DocumentContent = cmdInsert.Parameters.Add("@DocumentContent", SqlDbType.Int);
                   SqlParameter DocumentExt= cmdInsert.Parameters.Add("@DocumentExt", SqlDbType.VarChar, 10);
                   da.InsertCommand.Parameters.Add("@DocumentName", SqlDbType.VarChar).Value = name;
                   da.InsertCommand.Parameters.Add("@DocumentContent", SqlDbType.VarBinary).Value = documentContent;
                   da.InsertCommand.Parameters.Add("@DocumentExt", SqlDbType.VarChar).Value = extn;
    
                   cs.Open();
                   da.InsertCommand.ExecuteNonQuery();
                   cs.Close();
             } 
       }
    

    //另一种方式是

    if(FileUpload1.HasFile)//To check file os present
    {
        if(FileUpload1.PostedFile.ContentLength > 0)
                    {
                        FileInfo fi = new FileInfo(FileUpload1.FileName);
                        byte[] documentContent = FileUpload1.FileBytes;
    
                        string name = fi.Name;
                        string extn = fi.Extension;
                        SqlCommand cmdInsert = new SqlCommand("Insert into tableName(DocumentName,DocumentContent, DocumentExt) Values(@DocumentName,@DocumentContent,@DocumentExt)", con);
                        SqlParameter Name = cmdInsert.Parameters.Add("@DocumentName", SqlDbType.VarChar, 100);
                        SqlParameter DocumentContent = cmdInsert.Parameters.Add("@DocumentContent", SqlDbType.Int);
                        SqlParameter DocumentExt= cmdInsert.Parameters.Add("@DocumentExt", SqlDbType.VarChar, 10);
                        da.InsertCommand.Parameters.Add("@DocumentName", SqlDbType.VarChar).Value = name;
                        da.InsertCommand.Parameters.Add("@DocumentContent", SqlDbType.VarBinary).Value = documentContent;
                        da.InsertCommand.Parameters.Add("@DocumentExt", SqlDbType.VarChar).Value = extn;
    
                        cs.Open();
                        da.InsertCommand.ExecuteNonQuery();
                        cs.Close(); 
                   }
             }
    

    【讨论】:

    • (ListBox)FileUpload1有错误;无法将文件上传类型转换为列表框
    • 您还可以使用以下属性来检查文件是否存在: if(FileUpload1.HasFile)//检查文件是否存在 { if(FileUpload1.PostedFile.ContentLength > 0) { //你的代码。 } }
    • 我已经试过了。那么错误就是。错误:必须声明标量变量“@DocumentName”。在 da.InsertCommand.ExecuteNonQuery();
    • 以上方法可以试试。
    • 对于 ListBox 功能,您必须添加 Listbox cotrol 以及按钮和 FileUpload 控件。单击添加按钮时,必须将文件上传内容添加到列表框。使用此功能,您可以一次上传多个文件。
    猜你喜欢
    • 1970-01-01
    • 2017-03-18
    • 1970-01-01
    • 1970-01-01
    • 2020-02-15
    • 2015-08-10
    • 1970-01-01
    • 2013-01-10
    相关资源
    最近更新 更多