2015-11-19 38 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

相關問題