File already exist using system.io.file.copy - asp.net

I'm using System.IO.File.Copy to copy files from serverA to serverB. This works fine accept when the file exsist I get an error "File already exist". I tried trapping it using if file.exsist and nothing.
here is my code.
'Save files to disk
FileUpload1.SaveAs(Server.MapPath("../pdf/audits/" & FileName))
'Local Server
Dim localPath As String = "\\server01\folder1$\pdf\audits\"
'Remote Server
Dim remotePath As String = "\\server02\folder2$\pdf\audits\"
System.IO.File.Copy(localPath + FileName, remotePath + FileName)
What am I missing?

If you just modify your copy operation like this, it should work. The last parameter will overwrite the file.
System.IO.File.Copy(localPath + FileName, remotePath + FileName, True);

If you have large files, you are not going to want to overwrite them everytime. Try fixing your check to see if the file exist. Something like this (C#):
var localPath = #"C:\";
var remotePath = #"\\server\folder\";
var fileName = "test.txt";
if (!new System.IO.FileInfo(remotePath + fileName).Exists)
{
System.IO.File.Copy(localPath + fileName, remotePath + fileName);
}

There's a third parameter to overwrite if it already exists
System.IO.File.Copy(fileName, destName, overwrite);

I got it working with help from RLG.
'Save files to disk
FileUpload1.SaveAs(Server.MapPath("../pdf/audits" & FileName))
'SIGAR Public CMS
Dim localPath As String = "\\hqdadev01\sigar_cms$\pdf\audits\"
'SIGAR Dev
Dim remotePath As String = "\\hqdadev02\sigar_public$\pdf\audits\"
Added this to check.
If Not New System.IO.FileInfo(remotePath + FileName).Exists Then
File.Copy(localPath + FileName, remotePath + FileName, overwrite)
End If

Related

HttpPostedFile and SaveAs not saving a part of the path

I'm trying to save a file that is uploaded using an HttpPostedFile control.
The main issue I'm running into, is that it won't create a new folder for the file.
In the code below, "file" is an HttpPostedFile.
So I have my base path that I define like this:
Dim basePath = "D:\\game\\world\\map\\MediaFiles\\"
Then I get the file name like this:
Dim fileName = Path.GetFileName(file.FileName)
Now I want to create a new path like this using the gameId (guid):
Dim newFolderAndFile As String = gameId + fileName
And then combine the path with the base path and save:
Dim saveAsPath = Path.Combine(basePath, newFolderAndFile)
file.SaveAs(saveAsPath)
But when I try that, I always get an error like this:
System.IO.DirectoryNotFoundException: Could not find a part of the
path
'D:\game\world\map\MediaFiles\05a10e9c-e8a9-49ed-ad4f-34b6b4650ef3\5.jpg'
So it looks like the saveAsPath is being constructed correctly, however SaveAs isn't saving it.
How can I get SaveAs to create the path and file?
Thanks!
As the_lotus explained, you need to create the directory first before saving the file
Dim basePath = "D:\game\world\map\MediaFiles\"
Dim fileName = Path.GetFileName(file.FileName)
' make new folder
system.io.direcotry.createdirectory(basepath & gameid)
Dim saveAsPath = (basePath & gameid & "\" & filename)
file.SaveAs(saveAsPath)
I'm not sure about the double slashes, I think that's not needed...

Why i can not find file on the server for download in asp.net?

I'm beginner in asp.net and want to write simple web application to end user can download any file,write this code for that purpose:
string filePath = "~/beh/" + query[0].OstFileName;
FileInfo file = new FileInfo(filePath);
if (file.Exists) {
Response.ContentType = "application/octet-stream";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + query[0].OstFileName.Trim());
Response.TransmitFile(Server.MapPath("~/beh/" + query[0].OstFileName.Trim()));
Response.End();
} else {
Response.Write("<script>alert('File Not Found 1')</script>");
}
but when run that code i get else block alert,means get File Not Found 1 message,Where is my fault?thanks all.
string filePath = "~/beh/" + query[0].OstFileName;
In this line you specify the path to search for, including the filename.
The next line you check if this file exists and if so save the file from the query result.
However if you check if the file exists before you actually save the file it will never exist.
Change the path to read: string filePath = Server.MapPath("~\beh").
Then add the filename to that once you save the file.
Hope this makes sense.
Your issue is in the following lines.
string filePath = "~/beh/" + query[0].OstFileName;
FileInfo file = new FileInfo(filePath);
You're giving the FileInfo class a virtual path which the FileInfo cannot map to a local physical path on its own. You are however correctly mapping the virtual path to the local file path when you call Response.TransmitFile.
Change your code to the following:
string filePath = "~/beh/" + query[0].OstFileName;
FileInfo file - new FileInfo(Server.MapPath(filePath));
See: https://msdn.microsoft.com/en-us/library/ms524632(v=vs.90).aspx

DirectotyNotFoundexception when i upload image through Fileupload tool in Asp.net

i want to store the images to the ms SQL server,when i am submit the button after inserting the image i getting Directory Not Found exception the error.
Thanks in advance.
if(filuploadimages.PostedFile!=null)
{
string filename = filuploadimages.PostedFile.FileName.ToString();
string fileExt = System.IO.Path.GetExtension(filuploadimages.FileName);
filuploadimages.SaveAs(Server.MapPath("~/Productimages/" + filename));//error
}
Which path are you getting as result from this line?
Server.MapPath("~/Productimages/" + filename)
I suggest you to try to get the upload path in a different way:
if (filuploadimages.PostedFile != null)
{
string filename = System.IO.Path.GetFileName(filuploadimages.PostedFile.FileName);
string uploadPath = System.IO.Path.Combine(Server.MapPath("~/Productimages"), filename);
filuploadimages.SaveAs(uploadPath);
}

I am not able to save image into Folder

I am trying to save a image in specified folder but it doesn't saving image but code is working for excel file storing...did i missed something here....please suggestions needed.
string filename = Path.GetFileName(imgUploadControl.PostedFile.FileName);
string folder = Server.MapPath("~/Files/");
imgUploadControl.PostedFile.SaveAs(Path.Combine(folder, filename));
string filePath = Path.Combine(folder, filename);
imglogo.ImageUrl = Server.MapPath("~/Files/" + imgUploadControl.FileName);

how to get the full path of file upload control in asp.net?

i am using asp.net 2.0 in my project using file upload control , so browse the drive and select the file like path(D:\user doc new 2011\Montana\MT_SUTA_2010-2011.html)
but in my code seeing error is could not find the file path is
(D:\Paymycheck\OnlineTaxUserDocumentation-1\TaxesDocument\MT_SUTA_2010-2011.html) actually it is application path and take the filename only my code is
if (FileUpload.HasFile)
{
string filepath = Server.MapPath(FileUpload.FileName);
string strHTML = File.ReadAllText(filepath);
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
byte[] file1 = encoding.GetBytes(strHTML);
int len = file1.Length;
byte[] file = new byte[len];
docs.TaxAuthorityName = ddlTaxAuthority.SelectedItem.Text;
docs.TaxTypeID = ddlTaxType.SelectedValue;
docs.TaxTypeDesc = ddlTaxType.SelectedItem.Text;
docs.EffectiveDate = Convert.ToDateTime(txtEffectiveDate.Text);
docs.FileName = f1;
if (ddlTaxAuthority.SelectedValue == "FD")
{
docs.Add(strHTML, file1);
}
}
error occoured in this line
string strHTML = File.ReadAllText(filepath);
i can try like this also
string FolderToSearch = System.IO.Directory.GetParent(FileUpload.PostedFile.FileName).ToString();
string f = Path.GetDirectoryName(FileUpload.PostedFile.FileName);
string f1 = FileUpload.FileName;
string filepath = System.IO.Path.GetFullPath(FileUpload.PostedFile.FileName);
string strFilePath = FileUpload.PostedFile.FileName;
string file1234 = System.IO.Path.GetFullPath(FileUpload.PostedFile.FileName);
string filepath = FileUpload.PostedFile.FileName;
so how to get the total path drive to file
pls help me any one
thank u
hemanth
Because your are using Server.MapPath, which according to MSDN "maps the specified relative or virtual path to the corresponding physical directory on the server." You have to first call FileUpload.SaveAs method to save file on server then try to read it's content.
If you want the total client side drive path like D:\user doc new 2011\Montana\MT_SUTA_2010-2011.html for the file MT_SUTA_2010-2011.html to be uploaded via fileupload control, then try using System.IO.Path.GetFullPath(FileUpload1.PostedFile.FileName).
This will surely return the client side path of the file.
Try this it will work
string filepath = System.IO.Path.GetFullPath(fuldExcel.PostedFile.FileName);
fuldExcel.SaveAs(filepath); //fuldExcel---is my fileupload control ID

Resources