2011-07-26 37 views
1

私はプロジェクトをやっている格納します。私はAsyncFileUploadコントロールを使用してファイルをアップロードし、それを「〜/画像/」フォルダを保存しようとしていますはAsyncFileUploadコントロールを使用してファイルをアップロードし、それが

Front end - Visual Studio 2010 

Technology : C# 

Back end - Sql Server 2005 

スクリプト:背後に

<asp:UpdatePanel ID="UpdatePanel1" runat="server"> 
    <ContentTemplate> 
     <asp:ScriptManager ID="ScriptManager1" runat="server"> 
     </asp:ScriptManager> 
     <asp:AsyncFileUpload ID="AsyncFileUpload1" runat="server" 
      onuploadedcomplete="AsyncFileUpload1_UploadedComplete"/> 
    </ContentTemplate> 

コード:毎回その示すランタイムエラーが呼ば

protected void AsyncFileUpload1_UploadedComplete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e) 
{ 
    if (AsyncFileUpload1.HasFile) 
    { 
     AsyncFileUpload1.SaveAs("~/Image/" + AsyncFileUpload1.FileName); 
     Label2.Text = "Recieved " + AsyncFileUpload1.FileName + " Content Type" + AsyncFileUpload1.PostedFile.ContentType; 
    } 
} 

しかし:

The SaveAs method is configured to require a rooted path, and the path '~\Image\Filename.jpg' is not rooted. 

は私が知っているかもしれないエラーとそのソリュー。事前に

おかげで、

ニキル

+0

これは[正確な重複]です(http://stackoverflow.com/questions/1350977/the-saveas-method-is-configured-to-require-a-rooted-path-and-the-path)。イメージ) –

答えて

0

は、私はそれを考え出した:背後に

table width="100%" style="font: 8pt verdana"> 
       <tr width="100%"> 
       <td width="40%"> 
        <asp:FileUpload ID="FileUpload1" runat="server" /> 
        <asp:HiddenField ID="HiddenField1" runat="server" /> 
       </td> 
       <td width="40%"><asp:Label ID="lblPicStatus" runat="server"></asp:Label></td> 
       <td> 
       <asp:Button ID="Button2" runat="server" Text="Upload" BackColor="White" 
         BorderColor="#507CD1" BorderStyle="Solid" BorderWidth="1px" 
         onclick="Button2_Click"/></td> 
       </tr> 
</table> 

コード、

protected void Button2_Click(object sender, EventArgs e) 
{ 

    try 
    { 
     con.Open(); 
     if (FileUpload1.HasFile) 
     { 
      String fileExt = Path.GetExtension(FileUpload1.FileName); 
      if (fileExt == ".jpg" || fileExt == ".gif" || fileExt == ".bmp" || fileExt == ".jpeg" || fileExt == ".png") 
      { 
       String path = "~/Image/" + FileUpload1.FileName; 
       cmd.CommandText = "update " + HttpContext.Current.User.Identity.Name + " set image = '" + path + "'"; 
       cmd.Connection = con; 
       cmd.ExecuteNonQuery(); 
       FileUpload1.SaveAs(Server.MapPath("~/Image/") + FileUpload1.FileName); 
       Response.Redirect(Request.RawUrl); 
      } 
      else 
      { 
       lblPicStatus.Text = "File to be uploaded is not an image"; 
      } 
      con.Close(); 
     } 
    } 

    catch (Exception a) 
    { 
     Response.Write(a.Message); 
    } 
} 
0
String path = "~/Image/" + FileUpload1.FileName; 

は、このようにする必要があります:

String path = Server.MapPath("~/Image/") + FileUpload1.FileName; 
関連する問題