Avatar of coerrace
coerrace

asked on 

ASP:NET File Upload check if exist

Hello I have this routine to upload files with ASP.NET:
       String savePath = @"C:\inetpub\wwwroot\esp\docstemp1_1\";

         if (FileUpload1_1.HasFile)
        {

            string FileExtention = System.IO.Path.GetExtension(FileUpload1_1.FileName);
            if (FileExtention == ".pdf")
            {
           
                String fileName = FileUpload1_1.FileName;

                savePath += fileName;


     
                FileUpload1_1.SaveAs(savePath);

     

                pdfdocumento1_1.Text = "All fine";

   
            }

            else
            {
                pdfdocumento1_1.Text = "Is not .pdf. ";

            }

Now what I need to add and wehre to check if the new upload already exist on the dir to avoid overwrtie?
Thank you
ASP.NETC#Web Development

Avatar of undefined
Last Comment
coerrace
Avatar of mrjoltcola
mrjoltcola
Flag of United States of America image

if(System.IO.File.Exists(savePath))
   throw new Exception(...);
Avatar of prajapati84
prajapati84
Flag of India image

Try this:

As mrjoltcola suggested, you can check the existance of file at:
======================================================
String savePath = @"C:\inetpub\wwwroot\esp\docstemp1_1\";

         if (FileUpload1_1.HasFile)
        {
            string FileExtention = System.IO.Path.GetExtension(FileUpload1_1.FileName);
            if (FileExtention == ".pdf")
            {
                String fileName = FileUpload1_1.FileName;
                savePath += fileName;

                if(System.IO.File.Exists(savePath))
                       throw new Exception(...); //File already exists


                FileUpload1_1.SaveAs(savePath);
                pdfdocumento1_1.Text = "All fine";
            }
            else
            {
                pdfdocumento1_1.Text = "Is not .pdf. ";
            }
Avatar of coerrace
coerrace

ASKER

And in case exist how can i stop continue the script and return to request again the upload, colud yo clarify only a little the throw excpetion.
Thank you
Avatar of coerrace
coerrace

ASKER

to be more clear whatbhappen if the system ask if wan to overwrite yes or no if yes overwrite if no end and return.
Thank you
Avatar of mrjoltcola
mrjoltcola
Flag of United States of America image

The system wont ask you that. You have to put that logic in yourself.
Avatar of coerrace
coerrace

ASKER

I know the system don't do that but applied on my code an specific code of a logic what can be?
Thank you
Avatar of mrjoltcola
mrjoltcola
Flag of United States of America image

I don't understand.
SOLUTION
Avatar of oferam
oferam
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
ASKER CERTIFIED SOLUTION
Avatar of prajapati84
prajapati84
Flag of India image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of coerrace
coerrace

ASKER

Excellent that´s what I need. Thank you for all!!!
ASP.NET
ASP.NET

The successor to Active Server Pages, ASP.NET websites utilize the .NET framework to produce dynamic, data and content-driven web applications and services. ASP.NET code can be written using any .NET supported language. As of 2009, ASP.NET can also apply the Model-View-Controller (MVC) pattern to web applications

128K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo