2015-11-19 41 views
-1

大家好,我想问你如何控制提交表单,如果没有fileupload中的文件。我的上传文件的代码是这样的。如果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语句中放置什么。请帮帮我。

+0

另外考虑一个方面:可能是某人填充选择文件,但文件是空的。因此请检查文件长度。 FileUpload1.Content.Length> 0 – Khazratbek

回答

0

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

像:

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); 
} 

更新:我认为在我们的代码有问题。 U已使用

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); 
} 
+0

错误:必须声明标量变量“@DocumentName”。 On da.InsertCommand.ExecuteNonQuery(); – Edge

+0

我认为你正在调用上面的连接字符串如果(FileUpload1.FileName!=“”) {} –

+0

这个错误是因为,你的sql命令插入查询在{}之外。因此,您必须在{}内调用您的所有连接和命令字符串。它的东西就像if(FileUpload1.FileName!=“”) {//所有的代码都包含连接字符串,sql命令和da声明} –

0

我相信这将做的伎俩为您

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)方法。它检查文件名是否为空。如果没有选择文件,并且该值不是它不会去调用SQL的东西。

+0

错误:路径不是合法的形式。在FileInfo中显示fi = new FileInfo(FileUpload1.FileName); – Edge

0

在模型的属性上添加一个[必需]标签。您可以使用验证添加回复和其他内容。

+0

您的提示仅适用于MVC框架中的项目。对于网页形式,它会有所不同。下一次请考虑这方面请 – Khazratbek

0

我不知道你需要哪一种:

1.提交(回传)限制,添加在RequiredFieldValidator的

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

2.不要只保存图像:

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

希望这个帮助...

0

使用下面的场景:

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(); 
       } 
     } 
+0

(ListBox)FileUpload1中有一个错误;不能类型文件上传转换为列表框 – Edge

+0

也可以使用下面的属性,以检查文件存在或不存在:如果(FileUpload1.HasFile)//检查本文件OS { \t如果(FileUpload1.PostedFile.ContentLength> 0){ \t \t \t //你的代码。 \t \t} } – Amruta

+0

我已经试过了。那么错误是。错误:必须声明标量变量“@DocumentName”。 On da.InsertCommand.ExecuteNonQuery(); – Edge

相关问题