locked
Upload a file to your website issue RRS feed

  • Question

  • I have a button, when I click it, it will upload c:\test.jpeg, to my web address.

    I'm using the namespace as

    Using System.Net;

    Here is my button click code;

    WebClient  Client = new WebClient();
    Client.UploadFile("http://hesapmakinesi.freeiz.com", 
         "c:\deneme.jpg");

    I want it to pretend to be a FTP program to upload files to my website. But I think I'm absolutaley mistaken, because I'm not asked any ftp server name, user name and also password. Where am I mistaken?

    Thanks in advance.




    • Edited by ilerleartik Wednesday, April 25, 2012 8:55 PM
    Wednesday, April 25, 2012 7:55 PM

Answers

  • Hi Ilerleartik,

     The following sample code will upload a file and save it on the webserver. On the HTML view of the page, you need to place a Text box with a browse button which can be done with the following html tag:

    <INPUT id="filUpload" type="file" name="filUpload" runat="server"> 

    Now put a button on the screen and call it "cmdUpload", then double click the button to go to the click event and add this code in your code behind file:

    private void cmdUpload_Click(object sender, System.EventArgs e) 
    {
             string myFolder = "Images"
          if(base.Settings.fldAdsFolder.Length > 0)
          {
             string uploadPath = base.MappedApplicationPath + myFolder + @"\";
             this.litUploadError.Text = "Your file has been posted to the server.";
             //before actual upload so upload func can overwrite if error.
             UploadImage(uploadPath);
          }
          else
          {
             this.litUploadError.Text = "You must set the Ads Folder in Settings first.";
          }
     }
    private bool UploadImage(string UploadPath)
    {
       bool retVal = true;
       string strFileName;
       string strFilePath;
       string strType;
       string strFolder = UploadPath; //save it here
       string err = "";
       string ext = "";
       if(filUpload.Value != "")
       {
          strFileName = base.GetFileName(filUpload.PostedFile.FileName); 
          strType = filUpload.PostedFile.ContentType;
          bool allowedType = false;
          switch(strType)
          {
             case("image/gif"):
                   allowedType = true;
                   ext = ".gif";
                   break;
             case("image/jpg"):
                   allowedType = true;
                   ext = ".jpeg";
                   break;
             case("image/jpeg"):
                   allowedType = true;
                   ext = ".jpeg";
                   break;
            case("image/pjpeg"):
                   allowedType = true;
                   ext = ".jpeg";
                   break;
           case("application/x-shockwave-flash"): 
                   allowedType = true;
                   ext = ".swf";
                   break;
           default:
                   allowedType = false;
                   err = "The file is not an allowed file type (ie: gif, jpg, jpeg, swf)";
                   break;
          }
       if (allowedType) 
       {
          // Create the directory if it does not exist.
             if(!Directory.Exists(strFolder))
             {
                string strFolderToCreate = strFolder;
                if(strFolder.EndsWith(@"\"))
             strFolderToCreate = strFolder.Substring(0,(strFolderToCreate.Length-1));
             Directory.CreateDirectory(strFolderToCreate);
             }
             // Save the uploaded file to the server.
             strFilePath = strFolder + strFileName;
             if(File.Exists(strFilePath))
             {
                //only shows file name to user
                err = strFileName + " already exists on the server!";
             }
             else
             {
                filUpload.PostedFile.SaveAs(strFilePath);
             }
          }
          else //not allowed type, only images allowed
          {
             err = strFileName + " is not a valid image file.";
          }
       }
       if(err.Trim().Length > 0) 
       {
          this.litUploadError.Text = err.Trim();
          retVal = false;
       }
       return retVal; //if it returns as "false" don't continue to save. 
    }
    //You should really stick this function in a different class where you can reuse it better.
    public static string MappedApplicationPath
    {
       get
       {
          string APP_PATH = System.Web.HttpContext.Current.Request.ApplicationPath.ToLower();
          if(APP_PATH == "/")      //a site
             APP_PATH = "/";
          else if(!APP_PATH.EndsWith(@"/")) //a virtual
             APP_PATH += @"/";
          string it = System.Web.HttpContext.Current.Server.MapPath(APP_PATH);
          if(!it.EndsWith(@"\"))
             it += @"\";
          return it;
       }
    }



    Regards, http://shwetamannjain.blogspot.com

    Thursday, April 26, 2012 3:28 AM

All replies

  • If you want it to upload using FTP, you will need to specify a valid FTP location (ftp://).  Also, have you looked into specifying the credentials via the WebClient:

    http://msdn.microsoft.com/en-us/library/system.net.webclient.credentials.aspx

    Additionally, if you want to upload the file via the webpage, the destination URI must accept a POST (as outlined in the Remarks section of the following page):

    http://msdn.microsoft.com/en-us/library/36s52zhs.aspx

    Wednesday, April 25, 2012 11:19 PM
  • HI ilerleartik

    if you want to Upload file from client to FTP Server.

    please use simply smart FTP client Warper class FtpWebRequest

    see fellowing uri.

    http://msdn.microsoft.com/en-us/library/system.net.ftpwebrequest.aspx

    have a nice day.


    DON'T TRY SO HARD,THE BEST THINGS COME WHEN YOU LEAST EXPECT THEM TO.

    Thursday, April 26, 2012 1:45 AM
  • Hi Ilerleartik,

     The following sample code will upload a file and save it on the webserver. On the HTML view of the page, you need to place a Text box with a browse button which can be done with the following html tag:

    <INPUT id="filUpload" type="file" name="filUpload" runat="server"> 

    Now put a button on the screen and call it "cmdUpload", then double click the button to go to the click event and add this code in your code behind file:

    private void cmdUpload_Click(object sender, System.EventArgs e) 
    {
             string myFolder = "Images"
          if(base.Settings.fldAdsFolder.Length > 0)
          {
             string uploadPath = base.MappedApplicationPath + myFolder + @"\";
             this.litUploadError.Text = "Your file has been posted to the server.";
             //before actual upload so upload func can overwrite if error.
             UploadImage(uploadPath);
          }
          else
          {
             this.litUploadError.Text = "You must set the Ads Folder in Settings first.";
          }
     }
    private bool UploadImage(string UploadPath)
    {
       bool retVal = true;
       string strFileName;
       string strFilePath;
       string strType;
       string strFolder = UploadPath; //save it here
       string err = "";
       string ext = "";
       if(filUpload.Value != "")
       {
          strFileName = base.GetFileName(filUpload.PostedFile.FileName); 
          strType = filUpload.PostedFile.ContentType;
          bool allowedType = false;
          switch(strType)
          {
             case("image/gif"):
                   allowedType = true;
                   ext = ".gif";
                   break;
             case("image/jpg"):
                   allowedType = true;
                   ext = ".jpeg";
                   break;
             case("image/jpeg"):
                   allowedType = true;
                   ext = ".jpeg";
                   break;
            case("image/pjpeg"):
                   allowedType = true;
                   ext = ".jpeg";
                   break;
           case("application/x-shockwave-flash"): 
                   allowedType = true;
                   ext = ".swf";
                   break;
           default:
                   allowedType = false;
                   err = "The file is not an allowed file type (ie: gif, jpg, jpeg, swf)";
                   break;
          }
       if (allowedType) 
       {
          // Create the directory if it does not exist.
             if(!Directory.Exists(strFolder))
             {
                string strFolderToCreate = strFolder;
                if(strFolder.EndsWith(@"\"))
             strFolderToCreate = strFolder.Substring(0,(strFolderToCreate.Length-1));
             Directory.CreateDirectory(strFolderToCreate);
             }
             // Save the uploaded file to the server.
             strFilePath = strFolder + strFileName;
             if(File.Exists(strFilePath))
             {
                //only shows file name to user
                err = strFileName + " already exists on the server!";
             }
             else
             {
                filUpload.PostedFile.SaveAs(strFilePath);
             }
          }
          else //not allowed type, only images allowed
          {
             err = strFileName + " is not a valid image file.";
          }
       }
       if(err.Trim().Length > 0) 
       {
          this.litUploadError.Text = err.Trim();
          retVal = false;
       }
       return retVal; //if it returns as "false" don't continue to save. 
    }
    //You should really stick this function in a different class where you can reuse it better.
    public static string MappedApplicationPath
    {
       get
       {
          string APP_PATH = System.Web.HttpContext.Current.Request.ApplicationPath.ToLower();
          if(APP_PATH == "/")      //a site
             APP_PATH = "/";
          else if(!APP_PATH.EndsWith(@"/")) //a virtual
             APP_PATH += @"/";
          string it = System.Web.HttpContext.Current.Server.MapPath(APP_PATH);
          if(!it.EndsWith(@"\"))
             it += @"\";
          return it;
       }
    }



    Regards, http://shwetamannjain.blogspot.com

    Thursday, April 26, 2012 3:28 AM