ASP.NET - query - Asked By myzonal.com myzonal.com on 31-Jan-12 12:18 AM

Upload actual  image in folder and check duplicate image.
Jitendra Faye replied to myzonal.com myzonal.com on 31-Jan-12 12:23 AM
To upload file you can use FileUpload control like this-

use FileUpload control to upload a file

<form id="form1" runat="server">

<asp:FileUpload id="FileUploadControl" runat="server" />

<asp:Button runat="server" id="UploadButton" text="Upload" onclick="UploadButton_Click" />

<br /><br />

<asp:Label runat="server" id="StatusLabel" text="Upload status: " />

</form>

 

In the button click write this code

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;

}

}

}



Note - to check duplicate file check that file is exist or not in that folder using File.Exist() method.

Try and let me know.
Venkat K replied to myzonal.com myzonal.com on 31-Jan-12 12:28 AM

Here i am sharing the code which i already using.. In an high level you need to use File.Exists to check whether the file exists in the folder or not.


//First of all, clear data uploaded at previous time.

if (e.FirstUploaded)

{

InitGallery();

}


//Save file and thumbnail

string physGalleryPath = Server.MapPath(galleryPath);

string sourceFileName = e.SourceFile.FileName.ToString();

//If file with the same name exist, it will not be saved

if (!System.IO.File.Exists(sourceFileName))

{

e.SourceFile.Save(System.IO.

Path.Combine(physGalleryPath, sourceFileName));

}


private void InitGallery()

{



//Delete source files



DirectoryInfo dir = new DirectoryInfo(Server.MapPath(galleryPath));


foreach (FileInfo file in dir.GetFiles())

{



//file.Delete();


}



//Delete thumbnails


dir =

new DirectoryInfo(Server.MapPath(galleryPath) + "/Thumbnails");


foreach (FileInfo file in dir.GetFiles())

{



//file.Delete();


}



XmlDocument descriptions = new XmlDocument();


descriptions.AppendChild(descriptions.CreateElement(


"files"));


descriptions.Save(Server.MapPath(galleryPath +


"Descriptions.xml"));

}


Thanks

D Company replied to myzonal.com myzonal.com on 31-Jan-12 12:37 AM
Hello friend.


You can check for a file's existence with the System.IO.File.Exists method. It takes a path string as its argument.

For manipulating the path string should a file exist, It's a great utility for doing just what you need

//pass the path of file
Use these lines of code to do this.

if (System.IO.File.Exists("Path")) 
   
{
 
    //restrict user here, display message like image already exist

   
}
 

//and to save the file

fileUpload.SaveAs(path); 


let me know if you need more assistence on this!!
Regards
D
Riley K replied to myzonal.com myzonal.com on 31-Jan-12 01:16 AM


First check if the file already exists in the server, if yes then only upload it

sample code


If System.IO.File.Exists(Server.MapPath(fileul.FileName)) Then
MsgBox("File is already uploaded")
Else
fileul.SaveAs(Server.MapPath(fileul.FileName))
End If

Regards