且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在我的网站上的特定文件夹中上传图片

更新时间:2022-11-05 13:30:30

您需要提供相对路径才能在服务器上运行它.


you need to give relative path to run this on server.


myFile.PostedFile.SaveAs(server.mappath("Images\myphoto.png");


protected void UploadButton_Click(object sender, EventArgs e)
     {
         if(FileUploadControl.HasFile)
           {
     try
     {
         string filename = Path.GetFileName(FileUploadControl.FileName);
         FileUploadControl.SaveAs(Server.MapPath("~/") + filename);
         StatusLabel.Text = "Upload status: File uploaded!";
     }
     catch (Exception ex)
     {
         StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
     }
         }


     }


请检查下面提供的解决方案.确保您已经设置了必需的设置:

Please check the solution provided below. Make sure that you have already set the required settings:

protected void btnUploadPic_Click(object sender, EventArgs e)
{
    if(PicFileUploadCtrl.HasFile)
        {
        try
        {
            string strFileName = Path.GetFileName(PicFileUploadCtrl.FileName); // Get the name of the selected Pic
            PicFileUploadCtrl.SaveAs(Server.MapPath("~/") + strFileName );  // Save the selected Pic on the server
            lblStatus.Text = "Pic uploaded successfully!!!";
        }
        catch (Exception ex)
        {
            lblStatus.Text = "Error while uploading the Pic.";         

        }

}