ASP.Net FileUpload: Rename file name before saving if already exists - c#

I need change file name when users upload from file upload control.
If file name be duplicate it add count at file name but here it doesn't work.
I mean when I upload file i.e. 00076007-2013.pdf it saves in the host with this name 00076007-2013(0).pdf
But when again I want upload 00076007-2013.pdf in host it overwrite last (00076007-2013(0).pdf).
But I want if there was 00076007-2013.pdf file it saves with this name 00076007-2013(1).pdf in host.
How I can solve this problem.
My code below.
int count = 0;
string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
string FileExstention = Path.GetExtension(FileUpload1.FileName);
string[] files = Directory.GetFiles(Server.MapPath("/public/2Version/sk_PDF/"));
if (FileUpload1.HasFile)
{
if (FileExstention == ".pdf")
{
try
{
if (File.Exists(Server.MapPath("/public/2Version/sk_PDF/") + fileName.ToString()))
{
foreach (string s in files)
{
string filename = string.Empty;
filename = Path.GetFileName(s).Substring(0, Path.GetFileName(s).LastIndexOf("."));
if (filename.Contains("("))
{
filename = filename.Substring(0, filename.LastIndexOf("("));
}
if (filename == fileName.ToString().Trim())
{
count++;
}
FileUpload1.PostedFile.SaveAs(Server.MapPath("/public/2Version/sk_PDF/") + filename.ToString() + "(" + count.ToString() + ")" + FileExstention);
}
}
else
{
FileUpload1.PostedFile.SaveAs(Server.MapPath("/public/2Version/sk_PDF/") + fileName.ToString());
}
}
catch (Exception ex)
{
throw ex;
}
}
}
Edit #01
This is the output.
I have upload the 00076007-2013.pdf file for three times.
The first time OK, 00076007-2013.pdf.
The second time OK, 00076007-2013(0).pdf.
The last time he was saved 00076007-2013(0).pdf and not 00076007-2013(1).pdf

Please change this part :
if (filename.Contains("("))
{
filename = filename.Substring(0, filename.LastIndexOf("("));
}
With :
if (filename.Contains("("))
{
filename = filename.Substring(0, filename.LastIndexOf("("));
count++;
}
I hope I was helpful.

Related

Uploaded file not being saved to directory

I am trying to upload a file using devexpress save as function which works exact same as the standard asp.net uploader but i am getting the following error
Could not find a part of the path 'C:\Projects\fhs\fhs\Uploads\documents\VX00150\Barry Allen\Aperture - Signature Template.docx'.
UploadDirectory refers to a virutal directory setup in the web config which im getting via the property.
string UploadDirectory = WebConfigurationManager.AppSettings["uploadDirectory"].ToString();
Which contains the directory
<add key="uploadDirectory" value="~\Uploads\" />
But for the live of me I cannot see why the file is not saving to the server
protected void UploadControl_FileUploadComplete(object sender, FileUploadCompleteEventArgs e)
{
UploadControl.Enabled = false;
string id = Request.QueryString["Case"];
if (id != null)
{
CaseId = Guid.Parse(id);
OpenCase = _dal.GetCaseById(Guid.Parse(id));
}
string PersonId = Session["CurrentPersonalMainID"].ToString();
Personal = _dal.GetPersonalByPersonalId(new Guid(PersonId));
e.CallbackData = SavePostedFile(e.UploadedFile, OpenCase.CaseReference, Personal.firstName, Personal.lastName);
}
The below saved postedfile is called from above
public string SavePostedFile(UploadedFile uploadedFile,string IVACaseRef, string firstName ,string lastName)
{
try
{
if (!uploadedFile.IsValid)
return string.Empty;
string fileName = uploadedFile.FileName;
FileInfo fileInfo = new FileInfo(uploadedFile.FileName);
string fullFileName = CombinePath(fileName);
string docsPath = UploadDirectory + #"documents\" + IVACaseRef + #"\" + firstName + " " +
lastName + #"\";
string resFileName =docsPath + fileInfo.Name;
bool exists = System.IO.Directory.Exists(resFileName);
if (!exists)
System.IO.Directory.CreateDirectory(docsPath);
uploadedFile.SaveAs(Server.MapPath(resFileName), true);
System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment(resFileName.ToString());
// we need to reget this as issue with postback and the fileuplaod
FormsIdentity _identity = (FormsIdentity)Context.User.Identity;
_identity = (FormsIdentity)Context.User.Identity;
return fileName;
}
catch (Exception ex)
{
string inner = string.Empty;
if (ex.InnerException != null)
{
inner = ex.InnerException.ToString();
}
// logger.Error("Error in GetNotificationById function aperturenetdal " + ex.ToString() + " " + inner);
return "";
I've noticed that a lot of file manipulation methods and classes differ in how they handle spaces, sometimes in ways that are not well documented. For example, I've seen situations where testing for the existence of a file with a space in the filename succeeds but opening it does not.
It's just a hunch, but it seems likely.

how to get multiple files path in an array

i want to upload multiple pdf files in one file upload control in asp.net and then merg it ,
this is already done when i pass static path and file name
how to do that dynamically
my code is here
if (FileUpload1.HasFile)
{
try
{
HttpFileCollection uploadedVideoFiles = Request.Files;
// Get the HttpFileCollection
for (int i = 0; i < uploadedVideoFiles.Count; i++)
{
HttpPostedFile hpfiles = uploadedVideoFiles[i];
string fname = Path.GetFileName(hpfiles.FileName);
if (hpfiles.ContentLength > 0)
{
hpfiles.SaveAs(Server.MapPath("~/Images/") + Path.GetFileName(hpfiles.FileName));
hpfiles.SaveAs(Server.MapPath(Path.Combine(#"~/Images/", fname)));
string filepath = Server.MapPath(#"~/Images/");
string path = filepath + fname;
}
}
String[] files = #"C:\ENROLLDOCS\A1.pdf,C:\ENROLLDOCS\A#.pdf".Split(',');
MergeFiles(#"C:\ENROLLDOCS\New1.pdf", files);// merg is a method which merg 2 or more than 2 documents
}
catch (Exception ex)
{
Label1.Text = "The file could not be uploaded. The following error occured: " + ex.Message;
}
}
You will need to collect values of path in a List<string> and then pass the result to MergeFiles().
I don't quite follow your code (you'll need to clean it up a bit), but what you need is basically this:
var fileNames =
uploadedVideoFiles.
Select(uvf => {
var fileName = Path.GetFileName(hpfiles.FileName);
var destinatonPath = Path.Combine(Server.MapPath("~/images"), fileName);
uvf.SaveAs(destinatonPath);
return destinationPath;
}).
ToArray();
MergeFiles(#"C:\ENROLLDOCS\New1.pdf", fileNames);
Beware of duplicating file names in ~/images, though.

How to check the existence of the file in this file upload function?

I am trying to develop upload file function with security as my programming instructor asked me to do. I implemented it in such a way that it will check the size, file format and the existence of the file. The logic was working well except for checking the existence of the file. For example, when I tried to upload a file which is already existed, I will not get a message telling me that the file is already existed and I don't know why it is not working.
protected void UploadFile(object sender, EventArgs e)
{
if(FileUpload1.HasFile)
try
{
string[] validTypes = { "bmp", "gif"};
string ext = System.IO.Path.GetExtension(FileUpload1.PostedFile.FileName);
if (size < limit)
{
for (int i = 0; i < validTypes.Length; i++)
{
if (ext == "." + validTypes[i])
{
string path = #"~\Images\";
string comPath = Server.MapPath(path + "\\" + FileUpload1.FileName);
if (!File.Exists(comPath))
{
FileUpload1.PostedFile.SaveAs(comPath);
Label1.Text = "File uploaded";
}
else
{
Label1.Text = "Existed";
}
}
else
{
Label1.Text = "Invalid File." + string.Join(",", validTypes);
}
}
}
else
{
Label2.ForeColor = System.Drawing.Color.Red;
Label2.Text = "file is heavy";
}
}
catch (Exception ex)
{
Label2.Text = "The file could not be uploaded. The following error occured: " + ex.Message;
}
}
When I debugged the code, I found that it will execute the else statement, but instead of displaying it to the user, it will display the message in the outer else statement which is "Invalid File.". Why?
if (ext == "." + validTypes[i])
{
string path = #"~\Images\";
string comPath = Server.MapPath(path + "\\" + FileUpload1.FileName);
if (!File.Exists(comPath))
{
FileUpload1.PostedFile.SaveAs(comPath);
Label1.Text = "File uploaded";
}
else
{
Label1.Text = "Existed";
}
}
else
{
Label1.Text = "Invalid File." + string.Join(",", validTypes);
}
Also, my instructor told me that the following line causes a vulnerability called path traversal.
string path = #"~\Images\";
So how to prevent this security hole? ?Any ideas?
There is logical problem in you code.In the block
for (int i = 0; i < validTypes.Length; i++)
It will always run two time for each file.
What you can do you take a Boolean variable at set it to false.
Go inside the loop and if file found set boolean to true and use break statement.
At the end of loop check for the Boolean value and code accordingly.
Edit-1
Rather than looping through the array you can use like this
string[] stringArray = { "text1", "text2", "text3", "text4" };
string value = "text3";
int pos = Array.IndexOf(stringArray, value);
if (pos >- 1)
{
// the array contains the string and the pos variable
// will have its position in the array
}
In your case
string[] validTypes = { "bmp", "gif"};
string ext = System.IO.Path.GetExtension(FileUpload1.PostedFile.FileName);
int pos = Array.IndexOf(validTypes , ext );
if(pos>=0)
{
string path = #"~\Images\";
string comPath = Server.MapPath(path + "\\" + FileUpload1.FileName);
if (!File.Exists(comPath))
{
FileUpload1.PostedFile.SaveAs(comPath);
Label1.Text = "File uploaded";
}
else
{
Label1.Text = "Existed";
}
}
else
{
Label1.Text = "Invalid File." + string.Join(",", validTypes);
}

c# service renaming files!

I have a windows service , that takes files with metadata(FIDEF) and corresponding video file and , translates the XML(FIDEF) using XSLT .
I get the file directory listing for FIDEF's and if a video file of the same name exists it translates it. That works ok , but it is on a timer to search every minute. I am trying to handle situations where the same file name enters the input directory but is already in the output directory. I just have it changing the output name to (copy) thus if another file enters i should get (copy)(copy).mov but the service won't start with filenames of the same directory already in the output , it works once and then does not seem to pick up any new files.
Any Help would be great as I have tried a few things with no good results. I believe its the renaming methods, but I've put most of the code up in case its a clean up issue or something else.
(forgive some of the names just trying different things).
private void getFileList()
{
//Get FILE LIST FROM Directory
try
{
// Process Each String/File In Directory
string result;
//string filename;
filepaths = null;
filepaths = Directory.GetFiles(path, Filetype);
foreach (string s in filepaths)
{
for (int i = 0; i < filepaths.Length; i++)
{
//Result Returns Video Name
result = Path.GetFileNameWithoutExtension(filepaths[i]);
FileInfo f = new FileInfo(filepaths[i]);
PreformTranslation(f, outputPath + result , result);
}
}
}
catch (Exception e)
{
EventLog.WriteEntry("Error " + e);
}
}
private void MoveVideoFiles(String Input, String Output)
{
File.Move(Input, Output);
}
private string GetUniqueName(string name)
{
//Original Filename
String ValidName = name;
//remove FIDEF from filename
String Justname1 = Path.GetFileNameWithoutExtension(name);
//get .mov extension
String Extension2 = Path.GetExtension(Justname1);
//get filename with NO extensions
String Justname = Path.GetFileNameWithoutExtension(Justname1);
//get .Fidef
String Extension = Path.GetExtension(name);
int cnt = 0;
//string[] FileName = Justname.Split('(');
//string Name = FileName[0];
while (File.Exists(ValidName)==true)
{
ValidName = outputPath + Justname + "(Copy)" + Extension2 + Extension;
cnt++;
}
return ValidName;
}
private string getMovFile(string name)
{
String ValidName = name;
String Ext = Path.GetExtension(name);
String JustName = Path.GetFileNameWithoutExtension(name);
while(File.Exists(ValidName))
{
ValidName = outputPath + JustName + "(Copy)" + Ext;
}
return ValidName;
}
//Preforms the translation requires XSL & FIDEF name.
private void PreformTranslation(FileInfo FileName, String OutputFileName , String result)
{
string FidefName = OutputFileName + ".FIDEF";
String CopyName;
String copyVidName = outputPath + result;
XslCompiledTransform myXslTransform;
myXslTransform = new XslCompiledTransform();
try
{
myXslTransform.Load(XSLname);
}
catch
{
EventLog.WriteEntry("Error in loading XSL");
}
try
{ //only process FIDEF's with corresponding Video file
if (AllFidef == "no")
{
//Check if video exists if yes,
if (File.Exists(path + result))
{
//Check for FIDEF File Already Existing in the Output Directory.
if (File.Exists(FidefName))
{
//Get unique name
CopyName = GetUniqueName(FidefName);
copyVidName= getMovFile(copyVidName);
//Translate and create new FIDEF.
//double checking the file is here
if (File.Exists(outputPath + result))
{
myXslTransform.Transform(FileName.ToString(), CopyName);
File.Delete(FileName.ToString());
MoveVideoFiles(path + result, copyVidName);
}
////Move Video file with Corresponding Name.
}
else
{ //If no duplicate file exsists in Directory just move.
myXslTransform.Transform(FileName.ToString(), OutputFileName + ".FIDEF");
MoveVideoFiles(path + result, outputPath + result);
}
}
}
else
{
//Must have FIDEF extension
//Processes All FIDEFS and moves any video files if found.
myXslTransform.Transform(FileName.ToString(), OutputFileName + ".FIDEF");
if (File.Exists(path + result))
{
MoveVideoFiles(path + result, outputPath + result);
}
}
}
catch (Exception e)
{
EventLog.WriteEntry("Error Transforming " + "FILENAME = " + FileName.ToString()
+ " OUTPUT_FILENAME = " + OutputFileName + "\r\n" +"\r\n"+ e);
}
}
There is a lot wrong with your code. getFileList has the unneeded inner for loop for starters. Get rid of it. Your foreach loop has s, which can replace filepaths[i] from your for loop. Also, don't do outputPath + result to make file paths. Use Path.Combine(outputPath, result) instead, since Path.Combine handles directory characters for you. Also, you need to come up with a better name for getFileList, since that is not what the method does at all. Do not make your method names liars.
I would simply get rid of MoveVideoFiles. The compiler just might too.
GetUniqueName only works if your file name is of the form name.mov.fidef, which I'm assuming it is. You really need better variable names though, otherwise it will be a maintenance nightware later on. I would get rid of the == true in the while loop condition, but that is optional. The assignment inside the while is why your files get overwritten. You always generate the same name (something(Copy).mov.fidef), and as far as I can see, if the file exists, I think you blow the stack looping forever. You need to fix that loop to generate a new name (and don't forget Path.Combine). Maybe something like this (note this is untested):
int copyCount = 0;
while (File.Exists(ValidName))
{
const string CopyName = "(Copy)";
string copyString = copyCount == 0 ? CopyName : (CopyName + "(" + copyCount + ")");
string tempName = Justname + copyString + Extension2 + Extension;
ValidName = Path.Combine(outputPath, tempName);
copyCount++;
}
This generates something(Copy).mov.fidef for the first copy, something(Copy)(2).mov.fidef for the second, and so on. Maybe not what you want, but you can make adjustments.
At this point you have a lot to do. getMovFile looks as though it could use work in the same manner as GetUniqueName. You'll figure it out. Good luck.

Having some issues while using chilkat library for zipping/unzipping

I am using chilkat library for zipping some files and moving them to a another path. This works fine most of the time, but some times there is a tmp file made in path where my exe resides. Now I checked suddenly after 3 months,this files has gone to take 2 GB of hard disk. Following is my code:
public static bool MoveShopTRRFiles() {
//string fullfilepath;
Zip zip = new Zip();
bool unlocked = zip.UnlockComponent("abc");
if (!unlocked) {
//MessageBox.Show(zip.LastErrorText);
//return;
return false;
}
if (Directory.Exists(_TRRPath)) {
foreach (string filename in Directory.GetFiles(_TRRPath, "*.trr")) {
if (!File.Exists(_ShopTRRPath + "\\" + GetShopName(filename) + ".zip")) {
zip.NewZip(_ShopTRRPath + "\\" + GetShopName(filename) + ".zip");
} else {
zip.OpenZip(_ShopTRRPath + "\\" + GetShopName(filename) + ".zip");
}
try {
if(zip.GetEntryByName (filename.Substring(filename.LastIndexOf('\\') + 4))==null ){
zip.AppendOneFileOrDir(filename);
}
zip.WriteZipAndClose();
File.Delete(filename);
} catch {
}
}
return true;
} else
return false;
}
FOr now,i am deleting the temp files if any from the application folder.So this solves the problem for now.

Categories