2014-05-11 17 views
0

我正在製作一個ASP.Net VB Web應用程序,我試圖將文件上傳到服務器。有一個模式彈出窗口,在彈出窗口中它會顯示一個圖像(如果它存在),如果它不存在,您可以上傳圖像。麻煩的是,每當我點擊上傳按鈕時,都沒有文件上傳。頁面刷新並彈出消失。我只是想要它,所以至少文件上傳正常。ASP.Net VB文件上傳

這是彈出

<div id="openModal" class="modalDialog"> 
<div> 
    <a href="#close" title="Close" class="close">X</a> 
    <h2>Transaction Recipt</h2>Transaction ID: 
    <asp:Label ID="Label2" runat="server" Text=''></asp:Label> 
    <br /><br /> 

    <asp:Image ID="Image1" runat="server" /> 
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> 
    <br /><br /> 
    <br /> 
    <asp:FileUpload id="FileUploadControl" runat="server" /> 

<br /><br /> 
    <asp:Button ID="Button2" runat="server" Text="Upload" OnClick = "Button_Upload" CommandArgument = '<%# Eval("Row") %>' Width="75" /><br> 
    <asp:Label ID="Label3" runat="server" Text=""></asp:Label> 
</div> 

ASP代碼這是該文件的VB代碼上傳

Protected Sub Button_Upload(ByVal sender As Object, ByVal e As EventArgs) 
    If FileUploadControl.HasFile Then 
     Try 
      FileUploadControl.SaveAs("~\Images\" & _ 
       FileUploadControl.FileName) 
      Label1.Text = "File name: " & _ 
       FileUploadControl.PostedFile.FileName & "<br>" & _ 
       "File Size: " & _ 
       FileUploadControl.PostedFile.ContentLength & " kb<br>" & _ 
       "Content type: " & _ 
       FileUploadControl.PostedFile.ContentType 
     Catch ex As Exception 
      Label1.Text = "ERROR: " & ex.Message.ToString() 
     End Try 
    Else 
     Label1.Text = "You have not specified a file." 
    End If 
End Sub 

這是整個頁面的ASP代碼如果您需要它

<%@ Page Title="" Language="VB" MasterPageFile="~/Admin.master" AutoEventWireup="false" 

CodeFile="Ledger.aspx.vb" Inherits="Admin_Accounting_Ledger" %> 

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server"> 
    <link href="../../Styles/Admin.css" rel="Stylesheet" type="text/css" /> 
</asp:Content> 

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server"> 

    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
    SelectCommand="SELECT [Account] FROM [Accounts]"></asp:SqlDataSource> 
    <table style="width:100%;"> 
     <tr> 
      <td class="style1"> 
    <asp:DropDownList ID="DropDownList1" runat="server" ViewStateMode="Enabled" 
     Width="241px" AutoPostBack="True" DataSourceID="SqlDataSource1" 
    DataTextField="Account" DataValueField="Account"> 
    </asp:DropDownList> 
       <br /> 
      </td> 
      <td class="style2"> 
    <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" 
     BackColor="LightGoldenrodYellow" BorderColor="Tan" BorderWidth="1px" 
     CellPadding="2" DataSourceID="SqlDataSource3" ForeColor="Black" 
     GridLines="None" Height="30px" Width="241px"> 
     <AlternatingRowStyle BackColor="PaleGoldenrod" /> 
     <EditRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" /> 
     <Fields> 
      <asp:BoundField DataField="Column1" HeaderText="Account Total" ReadOnly="True" 
       SortExpression="Column1" /> 
     </Fields> 
     <FooterStyle BackColor="Tan" /> 
     <HeaderStyle BackColor="Tan" Font-Bold="True" /> 
     <PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue" 
      HorizontalAlign="Center" /> 
    </asp:DetailsView> 
       </td> 
      <td> 
       <asp:Button ID="Button1" runat="server" Text="Add Transaction" 
        BackColor="#CCCCCC" /> 
      </td> 
     </tr> 
     <tr> 
      <td class="style1"> 
       &nbsp;</td> 
      <td class="style2"> 
       &nbsp;</td> 
      <td> 
       &nbsp;</td> 
     </tr> 
     <tr> 
      <td class="style1"> 
       &nbsp;</td> 
      <td class="style2"> 
       &nbsp;</td> 
      <td> 
       &nbsp;</td> 
     </tr> 
    </table> 
    <asp:SqlDataSource ID="SqlDataSource3" runat="server" 
     ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
     SelectCommand="SELECT SUM(Amount) FROM Transactions WHERE ([Account] LIKE '%' + @Account + '%')"> 
     <SelectParameters> 
      <asp:QueryStringParameter Name="Account" QueryStringField="id" /> 
     </SelectParameters> 
    </asp:SqlDataSource> 
    <asp:ScriptManager ID="ScriptManager1" runat="server"> 
    </asp:ScriptManager> 
       <asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource2"> 
       <ItemTemplate> 
        <table style="width: 100%;"> 
         <tr> 
          <td align="center" width="150px"> 
           <%# Eval("Date")%> 
          </td> 
          <td align="center" width="200px"> 
           <%# Eval("Category")%> 
          </td> 
          <td align="center" > 
           <%# Eval("Amount")%> 
          </td> 
          <td align="Right" > 
           <asp:Button ID="Button2" runat="server" Text="Reciept" OnClick = "Button_Reciept" CommandArgument = '<%# Eval("Row") %>' Width="75" /><br> 
          </td> 
         </tr> 

        </table> 
       <br/> 
       </ItemTemplate> 
       </asp:Repeater> 


<div id="openModal" class="modalDialog"> 
    <div> 
     <a href="#close" title="Close" class="close">X</a> 
     <h2>Transaction Recipt</h2>Transaction ID: 
     <asp:Label ID="Label2" runat="server" Text=''></asp:Label> 
     <br /><br /> 

     <asp:Image ID="Image1" runat="server" /> 
     <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> 
     <br /><br /> 
     <br /> 
     <asp:FileUpload id="FileUploadControl" runat="server" /> 

    <br /><br /> 
     <asp:Button ID="Button2" runat="server" Text="Upload" OnClick = "Button_Upload" CommandArgument = '<%# Eval("Row") %>' Width="75" /><br> 
     <asp:Label ID="Label3" runat="server" Text=""></asp:Label> 
    </div> 
</div> 




    <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
     ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 


     SelectCommand="SELECT [Date], [Amount], [Category], [Row] FROM [Transactions] WHERE ([Account] LIKE '%' + @Account + '%') ORDER BY Date DESC"> 
     <SelectParameters> 
      <asp:QueryStringParameter Name="Account" QueryStringField="id" Type="String" /> 
     </SelectParameters> 
    </asp:SqlDataSource> 
</asp:Content> 

謝謝

回答

0

1-如果您使用更新面板,則應使用回發觸發器。在代碼背後你應該使用視圖狀態(屬性)來存儲文件名和路徑,然後將它添加到你的數據庫。 3-在你的情況,我認爲有一個地方,你選擇上傳的文件後回傳頁面 - 跟蹤代碼 -

基本上,你只能保存在數據庫上的擴展名和它自己的圖像解決方案,所以你可以把它作爲你說的..你可以這樣做(代碼正在使用員工圖像)

If .EmpImagePath <> Nothing Then 
       FileFullPath = .EmpImagePath 
       Dim imgURL As String = Convert.ToBase64String(ConvertImageFiletoBytes(.EmpImagePath)) 
       Dim imgSrc As String = String.Format("data:/image/gif;base64,{0}", imgURL) 
       EmpImg.ImageUrl = imgSrc 
      Else 
       If .Gender = "f" Then 
        EmpImg.ImageUrl = "~/images/no_photo_female.jpg" 
       Else 
        EmpImg.ImageUrl = "~/images/nophoto.jpg" 
       End If 

      End If 
+0

如果我寫這樣的C中的完整的文件路徑:\用戶\查爾斯\文檔\ Visual Studio 2010 \ WebSites \ cbwwp \ Images \然後它工作。但我想使用基於網站根目錄的東西,比如〜/ Images /任何建議? – user3612221

+0

@ user3612221您可以使用[Server.MapPath](http://msdn.microsoft.com/zh-cn/library/ms524632%28v=vs.90%29.aspx)或[HostingEnvironment.MapPath](http:/ /msdn.microsoft.com/en-us/library/system.web.hosting.hostingenvironment.mappath%28v=vs.110%29.aspx)。 –

+0

我編輯了我的答案, –