How to correctly use the ASP.NET FileUpload control

后端 未结 6 2007
遥遥无期
遥遥无期 2020-12-09 03:47

I\'m trying to use the FileUpload control in ASP.NET

Here\'s my current namespace setup:

using System;
using System.IO;
using System.Collections.Gene         


        
6条回答
  •  不知归路
    2020-12-09 04:02

    Old Question, but still, if it might help someone, here is complete sample



    In your Code-behind, C# code to grab file and save it in Directory

    protected void UploadFile(object sender, EventArgs e)
        {
            //folder path to save uploaded file
            string folderPath = Server.MapPath("~/Upload/");
    
            //Check whether Directory (Folder) exists, although we have created, if it si not created this code will check
            if (!Directory.Exists(folderPath))
            {
                //If folder does not exists. Create it.
                Directory.CreateDirectory(folderPath);
            }
    
           //save file in the specified folder and path
            FileUpload1.SaveAs(folderPath + Path.GetFileName(FileUpload1.FileName));
    
            //once file is uploaded show message to user in label control
            Label1.Text = Path.GetFileName(FileUpload1.FileName) + " has been uploaded.";
        }
    

    Source: File Upload in ASP.NET (Web-Forms Upload control example)

提交回复
热议问题