My App creates a Content.pdf file that links to Server.pdf and Client.pdf. This is the folder structure:
..\MyApp\Ressources\Content.pdf
..\MyApp\Ressources\Server\Server.pdf
..\MyApp\Ressources\Client\Client.pdf
Furthermore, the folders and files get burned on a CD/DVD or get backed up on a network drive - that causes me to use relative file paths. I use following code to create the Content.pdf:
relative WebLink:
var relativeFileLinkPath = "./" + Directory.GetParent(doc.Uri.LocalPath).Name + "/"+ doc.OutputFileName;
page.AddWebLink(pdfrect, relativeFileLinkPath);
relative FileLink:
var relativeFileLinkPath = "./" + Directory.GetParent(doc.Uri.LocalPath).Name + "/"+ doc.OutputFileName;
page.AddFileLink(pdfrect, relativeFileLinkPath);
Both work fine on local drive, but my issues are:
WebLinks do not work/open on network drive
FileLinks close the Content.pdf and replace it with the linked pdf file
Questions:
How can I modify the FileLinks to get opened in a new (PDF reader) instance/window?
Holding the CTRL-Key when clicking on the FileLink is an option but not a nice solution.
What’s the right syntax for relative PDF web links?
AddFileLink is the correct method for files sitting side by side in a folder.
To control whether a new window will be opened, a small modification of PDFsharp's PdfLinkAnnotation class will be needed. PDF supports a NewWindow attribute which PDFsharp cannot currently set.
Get the source for PDFsharp, locate "<</Type/Action/S/Launch/F<</Type/Filespec/F{0}>> >>" in PdfLinkAnnotation.cs and replace it with "<</Type/Action/S/Launch/NewWindow true/F<</Type/Filespec/F{0}>> >>".
Disclaimer: I did not test the proposed change - I hope it is syntactically correct and I hope it does what you want.
Disclaimer 2: This change is needed for PDFsharp 1.50 beta 3. Later versions may have support for that flag.
Related
I trying to change my directory which in my local c disk, but where errors says in the title. Is there any way aside from using Server.MapPath?. I'm using a ZipOutputStream nuget package.
I want to locate my directory in C: instead inside the project folder.
public FileResult DownloadZipFileSig(string FileId){
var fileName = "FilesDL".zip";
var tempOutPutPath = Server.MapPath(Url.Content("C:/Users/SDILAP2/Desktop/ID_Esig_Files")) + fileName;
using (ZipOutputStream s = new ZipOutputStream(System.IO.File.Create(tempOutPutPath)))
{
s.SetLevel(9);
byte[] buffer = new byte[4096];
List<string> stringList = FileId.Split(',').ToList();
List<string> tempList = new List<string>();
foreach (string str in stringList)
{
if (System.IO.File.Exists(Server.MapPath("C:/Users/SDILAP2/Desktop/ID_Esig_Files/" + str + ".jpeg")))
{
tempList.Add(Server.MapPath("C:/Users/SDILAP2/Desktop/ID_Esig_Files/" + str + ".jpeg"));
}
}
stringList = tempList;
for (int i = 0; i < stringList.Count; i++)
{
ZipEntry entry = new ZipEntry(Path.GetFileName(stringList[i]));
entry.DateTime = DateTime.Now;
entry.IsUnicodeText = true;
s.PutNextEntry(entry);
using (FileStream fs = System.IO.File.OpenRead(stringList[i]))
{
int sourceBytes;
do
{
sourceBytes = fs.Read(buffer, 0, buffer.Length);
s.Write(buffer, 0, sourceBytes);
} while (sourceBytes > 0);
}
}
s.Finish();
s.Flush();
s.Close();
}
return File(finalResult, "application/zip", fileName);
}
You might be not quite grasping how web URL's work, and how server.mappath() is to be used.
Web users:
When you have a web based url, then all html markup in a page, or even user supplied URL's are so called web based.
So, if you have a folder from the root of your web site say called MyUpLoads
Then that is just a folder in the web site path names.
eg:
www.mywebsite/UpLoadFiles/cat.jpg
And if you write html markup, then you can and could provide a URL to the above picute, or say with a html image control, you could set the ImageURL or "source" (src) to that file.
And if you using IIS (and not IIS express), then of course you can add what is called a virutal folder. Say some big server drive on ANOHTER computer on the same network.
So, that virtual folder could be anywhere on your network, and of course AGAIN for web HTML, or web URL's, again you use this format:
www.mysite/MassiveFolder/info.pdf
or maybe
localhost:5403/MyUpLoads/cat.jpg
However, in code behind?
ANY code behind (c# or vb.net) ALWAYS uses plane jane WINDOWS file paths.
These are valid full windows file names.
That means that code behind is 100% free to open/read/use/see/play with ANY file on the computer, and any file even on the computer network.
So when you use
server.mapPath("localhost:5403/MyUpLoads/cat.jpg")
Then the above is translated into a local plane jane DOS/WINDOWS file path!!!!
The above may well become
C:\Users\AlbertKallal\source\repos\CSharpWebApp\MyUpLoads\cat.jpg
So keep in mind:
web urls - HTML/asp markup in a page = web based syntax/path.
computer path: plane jane full path names like all windows software.
So, in your case?
var fileName = "FilesDL".zip";
var tempOutPutPath = #"C:/Users/SDILAP2/Desktop/ID_Esig_Files")) + fileName;
So you don't need nor want to user server.mappath, since that is ONLY for a given HTML or web based URL that you want to translate into the local computer file path system.
Since your path name(s) are already in that format, then no need is required.
in fact, keep in mind that you can use this fact to your advantage.
ANY folder (or a vitural folder) will appear in your valid URL's and path names (web based).
However, you might have some pdf's, or sensitive documents. So move that folder OUT of the root or web project folders.
Now, no valid URL's exist, or are even allowed.
However, code behind? It can run, see and use ANY file on your computer - and you use code behind to get those files - but the web site, web side of things has NO ability to use or see or get those files. And you can still do things like say provide a download button, but your code behind can fetch the file, read it and pump it out to the end user (stream the file).
So you only need (have) to use the Server.MapPath function WHEN the URL comes from the web site or html markup. This will translate that web based URL into a regular good old fashion full qualified windows file path name.
However, if you already have that full windows path name, then no URL translate to windows file path is required.
So, for the most part, your code behind can look at, see, grab and play with files on the server. Web users, or web based urls MUST be part of the folders in the web site, but no such restrictions exist for the code behind.
Now, when the code is deployed to a web server, often some file security rights on in place, but as a general rule, that web code behind is NOT limited nor restricted to JUST folders in the web site. Those valued URL's are a restriction for the users and web browsers, and as noted, often a folder outside of the web site is used for security purposes, since no possible valid web based paths can use/see or even resolve to file outside of the root starting folder of the web site.
So for those existing files, you don't need server.mappath.
I'm struggling to open a PDF file inside of Unity. Currently, my application will open up the folder location instead of opening the actual PDF itself.
I've tried using both System.Diagnostics.Process.Startand Application.OpenURL but they all act the same.
Right now, my code looks like:
Application.OpenURL(Application.dataPath + "/PDFS/" + pdfFile);
Now when I hard code in the file location like below, it opens up the PDF correctly:
Application.OpenURL("C:\\Users\\user\\Documents\\Locator\\Assets\\PDFS\\foo.pdf");
Normally I'd leave it hard coded, but I need to allow one button to open any PDF. How can I resolve this?
The string output of the two lines below are most likely not equal.
Application.OpenURL(Application.dataPath + "/PDFS/" + pdfFile);
Application.OpenURL("C:\\Users\\user\\Documents\\Locator\\Assets\\PDFS\\foo.pdf");
Ensure the paths are the same and you should get the result you expect.
See the documentation for Application.OpenURL here:
http://docs.unity3d.com/ScriptReference/Application-dataPath.html
If you read to the bottom, you'll notice:
"Note that the string returned on a PC will use a forward slash as a folder separator."
This likely the reason why you're getting different results.
Also note that the value of Application.OpenURL changes based on the platform.
string pdfURL = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + HttpRuntime.AppDomainAppVirtualPath + "Documents/keyboard-shortcuts-Visual-Studio-Code.pdf";
it return below url and run in browser
http://localhost:1727/Documents/keyboard-shortcuts-Visual-Studio-Code.pdf
thanks
upendra
pe someone can help me on this.
I've been googling & searching a lot regarding this issue.
I'm working on web application where I need to :
Click a button to get a folder/directory of files - Done with
javascript
Get list of available files is the selected folder/directory - Done
with javascript
Start uploading those file in the list into server folder - Failed!
As I understand my limitation so far :
I cannot programmatically assign a value to asp:FileUpload control -
readOnly
I cannot access those file in the list in FileInfo/FileStream - it's fail as it can't find the source file given from the list due to it's not
came from server directory
came from server directory
I cannot simply try to use File.Copy to upload the file - it's fail as it can't find the source file given from the list due to it's not
came from server directory
I've been getting error :
"Could not find a part of the path"
List of files in the folder, example :
File1.pdf
File2.png
File3.txt
File4.xls
File5.jpg
While in the server, I already create a folder named as "Temp" but the problem actually because system can't recognize the path of those file in the list.
This is part of my code :
string strPath = #"C:\Users\Administrator\Desktop\Sample";
string strListedFiles = txtList.InnerText.ToString();
string[] strFiles = strListedFiles.split(new Char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
foreach(string strFile in strFiles)
{
FileInfo fiFile = new FileInfo(strPath.ToString() + "\\" + strFile.Trim());
FileStream fsFile = fiFile.OpenRead(); //Failed at here
...
...
...
...
}
Perhaps you are looking for something which has been shown with demo in this tutorial.
This will allow you to upload multiple files concurrently.
Another alternative example with jQuery MultiFile is given here.
I have a weird case in a word automation program I'm developing using the Office Interop and C# 3.5
One task of the program is to copy any linked images in the word document to a different location and rewrite the Linked Source of those images to the new location.
Now, in one document, when I check the linked files (using Word 2010), it points an image to a location similar to images\image_file.jpg - So, the image is in a subfolder of the folder where the document is. That's totally correct.
But, when my program runs into that image, the LinkFormat.SourceFullName of that same image gives me a path on our local network, e.g. \\net-storage\customer\001 - customername\data\images\documents\image_file.jpg, without any correlation whatsoever to the actual image link I'm expecting.
What's going wrong here? How do I get the correct image source in my program?
Edit to sw_lasse: I'm sure this is a relative path, because (in other documents) after deleting the image in the relative path and updating the fields in word, the image is not found. So it's definitely a relative path.
Also, the two paths (network and relative) have no correlation to each other. The images on the network use a completely different folder hierarchy, so that's why there's a document subfolder, while it doesn't exist in the relative path.
I know your writing VSTO C# code and this is VBA, but I believe this explains the odd behaviour your seeing.
You cannot use:
myPicPath = Options.DefaultFilePath(wdPicturesPath)
... because if the user has inserted a picture from a different folder, the Default File Path returns that folder rather than the actual setting from the dialog box. Instead, you can use:
With Dialogs(wdDialogToolsOptionsFileLocations)
.Path = "PICTURE-PATH"
.Update
myPicPath = .Setting
If Not Right$(myPicPath, 1) = "\" Then
myPicPath = myPicPath + "\"
End If
MsgBox myPicPath
End With
Similarly, you cannot use:
myDocPath = Options.DefaultFilePath (wdDocumentsPath)
to get the default document path, because this returns the current FileOpen path, not the default documents path!!
Instead, use:
Dim myDocPath As String
myDocPath = Dialogs(wdDialogToolsOptionsFileLocations).Setting
'Add a "\" at the end of the path, unless the setting is already followed by a "\" -
'which it will be if the setting is set to a root folder
If Not Right$(myDocPath, 1) = "\" Then
myDocPath = myDocPath + "\"
End If
MsgBox myDocPath
These days I'm pretty good at converting VBA to C#, if you provide some code that doesn't work I'll convert this VBA to suit.
Ref: How to retrieve Word's default Documents path or Pictures path setting
My C# program uses a web browser control and I programmatically set its html property by laoding it from a html string variable. This almost works well ,but I noticed it lost the reference to the css file. I think a simplest solution is to make the path of the css file absolute,but I want it remains relative to the C# executable.So let me ask how to let the html reference to the css file in such a context.
Thank you in advance.
Edit: I am sorry . My c# application is a desktop one ,although it uses a web browser control.
Edit: Let me put some code. I first load the document from an html file then store it in a variable then for the 2nd time or later I load the document from the variable.
//first load
web_browser_control.Url = new Uri( dir + #"\HTML\default1.html" );
void wB2_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{ html_string = web_browser_control.DocumentText; }
//second load or later
web_browser_control.DocumentText = html_string;
You need to have a look at what path it THINKS its resolving, it would be best if you could please include some code, chances are its not resolving to where you think it is as its executing out of the bin directory (is your path correct relative to the path of the executable and the html),
For example is the html its looking at still where you think it is or is it now in a different directory so the relative paths are now obsolete ?
More code would be good and an indication of the control you are using.
Seems you can change property of css file to be "content" and make property "copy to output directory" - "always", so the wep app dll will be in the same directory with css file.
Or you can use MapPath method.