2013-10-16 46 views
0

我的照片我的表是多個圖像上傳代碼不工作?

Column Name Data Type Constraint 
PhotoID  Int   Primary key,auto increment 
PhotoName Varchar(100)  
ExtName  Varchar(100)  
PhotoType Varchar(100)  
PhotoSize Int 
TempleID Int   Foreign key with templeinfo 

和插入過程

create proc [dbo].[prcTemplePhoto] 
(
@PhotoName Varchar(100), 
@ExtName Varchar(100), 
@PhotoType Varchar(100), 
@PhotoSize int, 
@TempleID Int 
) 
as 
insert into TemplePhoto(PhotoName,ExtName,PhotoType,PhotoSize,TempleID) values (@PhotoName,@ExtName,@PhotoType,@PhotoSize,@TempleID) 
select @@IDENTITY 

我想一次上傳多張照片爲這個。我是從網上代碼上designside

<tr> 
     <td> 
      <asp:Label ID="lbltemplepic" runat="server" Text="Temple Photo"></asp:Label> 
     </td> 
     <td id="fileUploadarea"> 
      <div> 
       <div id="Div1"> 
        <asp:FileUpload ID="templeupload" runat="server" CssClass="fileUpload" /><br /> 
       </div> 
       <br /> 
       <div> 
        <input style="display: block;" id="btnAddMoreFiles" type="button" value="Add more images" onclick="AddMoreImages();" /><br /> 
        <asp:Button ID="btnuplaod" runat="server" Text="Upload" OnClick="btnuplaod_Click" /> 
       </div> 
      </div> 
     </td> 
     <td></td> 
    </tr> 
<tr><td></td><td> 
    <asp:Button ID="btninsert" runat="server" Text="Insert" OnClick="btninsert_Click" /> 
    </td><td></td></tr> 
    <tr><td></td><td> 
     <asp:Label ID="lblerror" runat="server" Text=""></asp:Label></td><td></td></tr> 
        <script lang="javascript" type="text/javascript"> 
         function AddMoreImages() { 
          if (!document.getElementById && !document.createElement) 
           return false; 
          var fileUploadarea = document.getElementById("fileUploadarea"); 
          if (!fileUploadarea) 
           return false; 
          var newLine = document.createElement("br"); 
          fileUploadarea.appendChild(newLine); 
          var newFile = document.createElement("input"); 
          newFile.type = "file"; 
          newFile.setAttribute("class", "fileUpload"); 

          if (!AddMoreImages.lastAssignedId) 
           AddMoreImages.lastAssignedId = 100; 
          newFile.setAttribute("id", "FileUpload" + AddMoreImages.lastAssignedId); 
          newFile.setAttribute("name", "FileUpload" + AddMoreImages.lastAssignedId); 
          var div = document.createElement("div"); 
          div.appendChild(newFile); 
          div.setAttribute("id", "div" + AddMoreImages.lastAssignedId); 
          fileUploadarea.appendChild(div); 
          AddMoreImages.lastAssignedId++; 
         } 
         </script> 

and on code side

if (templeupload.HasFile) 
       { 
        TemplePhoto tempphoto = new TemplePhoto(); 
        tempphoto.PhotoName = templeupload.FileName; 
        tempphoto.PhotoSize = templeupload.PostedFile.ContentLength; 
        tempphoto.PhotoType = templeupload.PostedFile.ContentType; 
        tempphoto.ExtName = tempphoto.PhotoName.Substring(tempphoto.PhotoName.LastIndexOf(".") + 1); 
        tempphoto.TempleID = ans; 
         HttpFileCollection hfc = Request.Files; 
         for (int i = 0; i < hfc.Count; i++) 

         { 
           HttpPostedFile hpf = hfc[i]; 
           if (hpf.ContentLength > 0) 
           { 
        int id = new DBTemplePhoto().InsertData(tempphoto); 
        if (id != 0) 
        { 
            hpf.SaveAs(Server.MapPath("~/templepics/") + ans.ToString() + "." + tempphoto.ExtName); 
            lblerror.Text = " File is Uploaded "; 
           } 
            else 
        { 
         lblerror.Text = "Please check all the fields"; 
        } 
          } 
         } 

但它只能上傳一個PIC

其實我想多圖片上傳就像Gmail,但我讓步了這一點。 你可以幫我解決代碼上傳錯誤的問題嗎? 如果可能的話,請告訴代碼即可獲得如Gmail上傳

+0

對我來說,這可能是一個瀏覽器問題。你能告訴我你使用了什麼瀏覽器嗎?我知道IE10不支持多文件上傳... – deostroll

+0

Google Chrome .. – VJain

+0

正在使用什麼版本的asp.net?你在做ctrl +選擇一個文件...選擇多個文件? – deostroll

回答

0

嘗試的multipart/form-data的添加到您的形式在服務器端:

this.Form.Enctype = "multipart/form-data"; 

不要重用 「templeupload」 的屬性,只使用「HttpPostedFile HPF 「來自循環的屬性。

爲了更好的多文件上傳,檢查JqueryFileUpload插件: https://github.com/i-e-b/jQueryFileUpload.Net

0

有允許多文件上傳屬性上的文件上傳控件設置:

<asp:FileUpload ID="templeupload" runat="server" CssClass="fileUpload" 
    AllowMultiple="true" /> 

在你必須遍歷服務器PostedFiles集合。

foreach(HttpPostedFile hpf in templeupload.PostedFiles) 
{ 
    //do stuff with hpf 
    hpf.SaveAs(...); 
}