Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my project(C# windows application) i hav to upload a pdf from USB or harddisk and again i need to store the same pdf file in a specific folder.how to save it?
I hav a textbox which displays the uploaded file path. now am trying to save the file which is in the textbox. but the saved file contains the text as the path which diaplayed in the text box. how to save the file ??
Posted
Comments
Subhrajit Naha 25-Aug-12 4:12am    
Where you'r uploading your file? and in what format?
A.Anitha 25-Aug-12 4:16am    
pdf format.. uploading from usb to form and form to harddisk
Subhrajit Naha 25-Aug-12 4:22am    
What exactly you meant by uploading from USB to Form? Are you converting the file in ByteArray using FileStream? Or are you just specifying the file path using a OpenFileDialog?
A.Anitha 25-Aug-12 4:23am    
just specifying the file path using openfiledialog

That is not uploading, but specifying the location of the file. You don't need to bother about Uploading and Saving. If you just want to save that file to another location, use the following code:

C#
string FileToCopy = null;
string NewCopy = null;

FileToCopy = "C:\\Users\\Owner\\Documents\\test.txt";
NewCopy = "C:\\Users\\Owner\\Documents\\NewTest.txt";


if (System.IO.File.Exists(FileToCopy) == true) {
    System.IO.File.Copy(FileToCopy, NewCopy);
    Interaction.MsgBox("File Copied");

}


you need to import system.io as
C#
using System.IO;


Initialize the original path to FileToCopy variable and specify the Intended location to save the file in NewCopy variable.
 
Share this answer
 
Comments
A.Anitha 25-Aug-12 4:35am    
ya i got.. thank u...
ridoy 25-Aug-12 5:31am    
right one..+5 from me..
You have to rename the file name before saving it.

If your uploaded filename is like file.pdf , when trying to save split the file name till you find character '.' and append some text next to file name (like _new) and concatenate filename and extension again and save it like file_new.pdf.

If you are using fileUpload then try like this

C#
if (fileUpload1.HasFile)
{
string strPath = Server.MapPath("~");
strPath = strPath + "\\Resumes\\" + txtUsername.Text;
Directory.CreateDirectory(strPath);
strPath = strPath + "\\";
fileUpload1.SaveAs(strPath + fileUpload1.FileName);
}


If you want to use savefiledialog, then check this link
http://www.c-sharpcorner.com/uploadfile/mahesh/savefiledialog-in-C-Sharp/[^]

If you want to copy files,
then
http://msdn.microsoft.com/en-us/library/cc148994.aspx[^]
http://www.dotnetperls.com/file-copy[^]
 
Share this answer
 
v5
Comments
A.Anitha 25-Aug-12 4:22am    
i need the code to save the file i hav uploaded.
Santhosh Kumar Jayaraman 25-Aug-12 4:25am    
Check my updated solution
A.Anitha 25-Aug-12 6:04am    
thank u..
Santhosh Kumar Jayaraman 25-Aug-12 6:05am    
is that working fine?
A.Anitha 25-Aug-12 6:07am    
i tried someother code....

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900