Web Hosting Forum | Lunarpages


*
Welcome, Guest. Please login or register.
Did you miss your activation email?



Login with username, password and session length
May 16, 2012, 05:36:50 PM

Pages: [1]   Go Down
  Print  
Author Topic: How to upload file?  (Read 12107 times)
Lucky:)
Space Explorer
***
Offline Offline

Posts: 7


« on: January 26, 2011, 11:07:52 PM »

Hi,
I need an advise how to upload image file into website directory.

I'm trying

 
Code:
upload.SaveAs(Server.MapPath("/Upload/")+filename);

and getting error: Access is denied.

Thanks.
Logged
almedajohnson
Trekkie
**
Offline Offline

Posts: 13


WWW
« Reply #1 on: February 06, 2011, 08:37:16 PM »

Try this code:

The fileupload.aspx code is:

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

 <title>File Upload Demostration</title>

</head>

 <body>

    <form id="Form1" method="post" runat="server">

     <table cellpadding="0" cellspacing="0" width="80%" align="center" border="4">

      <tr><td height="20px"></td></tr>

       <tr><td height="200px" align="center" valign="middle">

               <input id="MyFile"   type="file" size="81" name="File1" runat="server" />

                      <br /><br />

                             <asp:Button id="btnSubmit"   runat="server" Text="Submit" Width="139px" Height="30px" OnClick="btnSubmit_Click"></asp:Button>

                             <asp:Label id="lbl"   runat="server" Width="402px" Height="33px"></asp:Label>

                           

                             </td></tr></table>

                   </form>

</body>

</html>

The fileupload.aspx.cs is:

using System;

using System.Data;

using System.Configuration;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

 

public partial class _Default : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

 

    }

    protected void btnSubmit_Click(object sender, EventArgs e)

    {

       

        if (MyFile.PostedFile.ContentLength == 0)

        {

            lbl.Text = "Cannot upload zero length file";

            return;

        }

        lbl.Text = MyFile.PostedFile.FileName;

       MyFile.PostedFile.SaveAs("c:\\UploadFile\\MyFile.PostedFile.FileName");

 

    }

}
Logged

Lucky:)
Space Explorer
***
Offline Offline

Posts: 7


« Reply #2 on: March 07, 2011, 09:47:11 PM »

Thanks. almedajohnson.
Logged
Pages: [1]   Go Up
  Print  
 
Jump to: