Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File Uploading using Server.MapPath() and FileUpload.SaveAs()

Tags:

c#

asp.net

I have a website admin section which I'm busy working on, which has 4 FileUpload controls for specific purposes. I need to know that , when I use the Server.MapPath() Method Within the FileUpload control's SaveAs() methods, Will it still be usable on the web server after I have uploaded the website? As far as I know, SaveAs() requires an absolute path, that's why I map a path with Server.MapPath()

if (fuLogo.HasFile) //My FileUpload Control : Checking if a file has been allocated to the control
        {
            int counter = 0;  //This counter Is used to ensure that no files are overwritten.
            string[] fileBreak = fuLogo.FileName.Split(new char[] { '.' });
            logo = Server.MapPath("../Images/Logos/" + fileBreak[0] + counter.ToString()+ "." + fileBreak[1]);  // This is the part Im wondering about. Will this still function the way it should on the webserver after upload?
            if (fileBreak[1].ToUpper() == "GIF" || fileBreak[1].ToUpper() == "PNG")
            {
                while (System.IO.File.Exists(logo))
                {
                    counter++; //Here the counter is put into action
                    logo = Server.MapPath("../Images/Logos/" + fileBreak[0] + counter.ToString() + "." + fileBreak[1]);
                }
            }
            else
            {
                cvValidation.ErrorMessage = "This site does not support any other image format than .Png or .Gif . Please save your image in one of these file formats then try again.";
                cvValidation.IsValid = false;
            }
            if (fuLogo.PostedFile.ContentLength > 409600 )  //File must be 400kb or smaller
            {
                cvValidation.ErrorMessage = "Please use a picture with a size less than 400 kb";
                cvValidation.IsValid = false;

            }
            else
            {

                if (fuLogo.HasFile && cvValidation.IsValid)
                {
                    fuLogo.SaveAs(logo); //Save the logo if file exists and Validation didn't fail. The path for the logo was created by the Server.MapPath() method.
                }

            }
        }
        else
        {
            logo = "N/A";
        }
like image 257
Eon Avatar asked Apr 28 '11 09:04

Eon


People also ask

What is Server MapPath in C#?

The MapPath method maps the specified relative or virtual path to the corresponding physical directory on the server.

How to upload file in GridView in asp net?

Net C# using File Upload Control and Display in Gridview. Launch/Open Visual Studio -> Go to File Menu -> New -> Project. Select Visual C# from left side template then choose web -> ASP.Net web application and name it then click on Ok -> Select Empty tempate -> click on Ok.

Which of the following methods is used to save the contents of an uploaded file?

The SaveAs method saves the contents of an uploaded file to a specified path on the Web server.


2 Answers

  • If you intend to save the files in a directory on your web server , then the Server.MapPath() will be the suitable solution.

    string dirPath = System.Web.HttpContext.Current.Server.MapPath("~") + "/Images/Logos/"+ fileBreak[0] + counter.ToString() + "." + fileBreak[1];

    Look Here

  • if you intend to save your files out the web server then

    Use a full path, like "c:\uploads" and be sure that the web process has permission to write to that folder,I suggest you store the path itself in the web.config file in this case.

like image 68
Anyname Donotcare Avatar answered Sep 19 '22 08:09

Anyname Donotcare


yes, that can be used after saving file and when you try retrieve that file...

Server.MapPath("~/Images/Logos/" + uploadedFileName);
like image 40
Muhammad Akhtar Avatar answered Sep 18 '22 08:09

Muhammad Akhtar