I want to copy a pdf to my own server but i get an error about path.
the codes like that:
string filepath = System.IO.Path.GetFullPath(open.FileName);
when i pick the pdf I want to save it to my server with this codes:
string filename = "New name" + ".pdf";
string newpath = #"\\WDMyCloud\Certificates\" + filename;
File.Copy(filepath, newpath, true);
But when i run this code i take an error like "invalid path name.
Related
I have crystal reports (using C#) that I pull and want to export to Excel. I manage to pull and export the report just fine. But in the code I manually specify the export directory.
Code:
myEquipCalRPT.ExportToDisk(ExportFormatType.Excel, "C:\\Temp\\ReportName.xls");
I know the path must be a string value. How can I ask the user where he/she wants to export the report to (and give it a name) instead of manually specifying the path in the code?
I tried using "SaveFileDialog" with the code below and then I get this exception: "Additional information: The given path's format is not supported.". This allows the user to specify a file name, although I noticed the file type just below the file name box is not set, and have no types to select from either.
SaveFileDialog browser = new SaveFileDialog();
string directoryPath = "";
ienter code here`f (browser.ShowDialog() == DialogResult.OK)
{
directoryPath = browser.ToString(); // prints path
}
myEquipCalRPT.ExportToDisk(ExportFormatType.Excel, directoryPath);
If I use "FolderBrowserDialog", I get the error: "Additional information: Access to the path 'C:\Directory' is denied." which I suspect is due to only giving a directory path, but no file name.
FolderBrowserDialog browser = new FolderBrowserDialog();
string directoryPath = "";
if (browser.ShowDialog() == DialogResult.OK)
{
directoryPath = browser.SelectedPath; // prints path
}
myEquipCalRPT.ExportToDisk(ExportFormatType.Excel, directoryPath);
If I try the following, I get no error, but also no file is saved:
FolderBrowserDialog browser = new FolderBrowserDialog();
string directoryPath = "";
string FileName = "ExcelExport.xls";
string Path = directoryPath + FileName;
if (browser.ShowDialog() == DialogResult.OK)
{
directoryPath = browser.SelectedPath; // prints path
}
myEquipCalRPT.ExportToDisk(ExportFormatType.Excel, Path);
Also, how can I export to .xlsx instead of .xls? I can specify the extension in the path as .xlsx, but it does not want to open. Need to remove the X at the end to make it work.
I ended up with this code to get the path correct as the path was given through as "System.Windows.Forms.SaveFileDialog: Title: , FileName: C:\Temp\test.xls" and not "C:\Temp\test.xls" with all the dialog boxes.
SaveFileDialog browser = new SaveFileDialog();
string directoryPath = "";
if (browser.ShowDialog() == DialogResult.OK)
{
directoryPath = browser.FileName.ToString();
}
And the you can use one of these lines to write the file, depending on which way you chose to use export the report:
myRPT.ExportToDisk(ExportFormatType.Excel, "C:\\Temp\\AMCMaintTempExcelReportFile.xls")
CrDiskFileDestinationOptions.DiskFileName = directoryPath;
myRPT.ExportToDisk(ExportFormatType.Excel, directoryPath);
I am trying copy a file from a network share (drive letter\folder) to a SharePoint document library using asp.net (C#) when I run the code locally and select a drive letter to copy from everything works fine, but when I publish it to IIS I get an error - Could not find a part of the path 'L:\Test\Test.Doc'. I am guessing this is because the drive mapping doesn't exist from IIS.
The code to get the file is:
if (FileUpload1.HasFile)
{
string spSite = System.Configuration.ConfigurationManager.AppSettings["SharePointSite"];
string RelativePath = System.Configuration.ConfigurationManager.AppSettings["DocumentLibraryRelativePath"];
SharePointIntegration sp = new SharePointIntegration();
string fileExtension = Path.GetExtension(FileUpload1.PostedFile.FileName);
string fileName = Path.GetFileNameWithoutExtension(FileUpload1.PostedFile.FileName);
string filePath = Path.GetDirectoryName(FileUpload1.PostedFile.FileName) + "\\" + fileName + fileExtension;
sp.UploadDocument(spSite, RelativePath, "Documents", filePath, fileName, recordID.ToString(), (string)HttpContext.Current.Session["UserName"], fileExtension);
GetAttachments();
}
I am using the SaveBinaryDirect method to save the file in SharePoint - sp.UploadDocument(spSite, RelativePath, .......).
What do I need to change to get the file when it is running on IIS?
this code is to check does the user has upload any file and the file namd.
if (FileUpload1.HasFile) {
fileName = FileUpload1.FileName;
FileUpload1.PostedFile.SaveAs(Server.MapPath("~/Data/") + fileName);
fileLocation = Server.MapPath("~/Data/") + fileName;
}
I wonder can I add the current date on the end of the file name. my file can be any type of file. I do try add on FileName= FileUpload1.FileName+updateon; but the file will save as example.jpg11062015 and the file will be corrupted.
anyone has any idea how to do it?
You can use Path to separate FileName into a file name and an extension, insert the date in the middle, and combine them back, like this:
var fn = Path.GetFileNameWithoutExtension(FileUpload1.FileName);
var ext = Path.GetExtension(FileUpload1.FileName);
var fileName = string.Format("{0}{1:yyyy-MM-dd}.{2}", fn, DateTime.Now, ext);
Try parsing the file name into parts, and inserting your date in between, like this:
var newFileName = Path.GetFileNameWithoutExtension(FileUpload1.FileName)
+ "11062015" // or wherever the date is coming from
+ Path.GetExtension(FileUpload1.FileName);
Original file name: "someFile.ext"
Modified file name: "someFile11062015.ext"
i wanna create backup data in my application I used saveFileDialog, so i can place backup file anywhere i want (Dekstop, drive D, etc)
my backup file will be db, image, video so i guess it's will be easier to place that in one folder let say it's "myBackup" folder (generate automatically with C#)
so if user wanna save in Dekstop all of backup data will be in ~C:\Users\Maju\Desktop\myBackup~
i already successfully generate folder but my file won't save inside that
mySaveFileDialog.FileName = "Backup Database " + dateTimeNow;
if (mySaveFileDialog.ShowDialog() == DialogResult.OK)
{
string fileAsal = System.IO.Path.Combine(Global.myDatabaseLocation, "data.mdb");
FileInfo fi = new FileInfo(mySaveFileDialog.FileName);
string nameFolder = "myBackup";
System.IO.Directory.CreateDirectory(#fi.DirectoryName + "\\" + nameFolder);
string path = System.IO.Path.Combine (fi.DirectoryName, "\\" + nameFolder);
string pathDestination = System.IO.Path.Combine(path, mySaveFileDialog.FileName);
System.IO.File.Copy(fileAsal, pathDestination, true);
}
Is not it easier to use FolderBrowserDialog?
mySaveFileDialog.FileName already includes the path to the file so you need to write
string pathDestination = System.IO.Path.Combine(path, System.IO.Path.GetFileName(mySaveFileDialog.FileName));
I am trying to get a path for a file which a users uploads but the path that I am getting is wrong because the path should be for an example
"C:\\Users\\Tranga.Patel\\Downloads\template.xlsx"
but on my program I get
"c:\\windows\\system32\\inetsrv\\Template Final.xlsx"
the code that I am using is
fileName = Path.GetFullPath(fileUpload.PostedFile.FileName);
I also tried using
fileName = Server.MapPath(Path.GetFullPath(fileUpload.PostedFile.FileName));
this give the directory of the project
try using following:
var path=Server.MapPath('Uploads folder path from root directory');
This gives you the folder path from the root directory of your website.
EDIT:- You should be knowing to which path users are saving the file if it is not in your site directory tree.
Are you using a File Upload control? If you are, you just need them to select the document they want to upload and then specify the path you want to save it at. For example
// Get File Name
documentName = Path.GetFileName(fuContentDocuments.FileName);
// Specify your server path
string serverPath = Server.MapPath("../../" + WebConfigurationManager.AppSettings["FilePath"].ToString());
// The final path
string fileLocation = (serverPath + "\\" + userId + "\\" + documentName);
// if folder doesn't exist then create it
if (!Directory.Exists(serverPath + "\\" + userId + "\\"))
{
// create the folder for the file
Directory.CreateDirectory(serverPath + "\\" + userId + "\\");
}
// Upload the file
fuContentDocuments.SaveAs(fileLocation);
Note: UserId is just the users login userId. This way other users wont override it.