I'm trying to retrieve a file from the Process.Start()-Method.
Following scenario is given:
I obtain a link from a third party program. This link can be a UNC-path, direct link to PDF/JPG/TIF-files, such as "www.certainServer.de/test.pdf", as well as link to programs on the server, which return a special file type, such as "www.certainServer.de/test.aspx".
I open the file like this
Process.Start(_path)
If the link is like "www.certainServer.de/test.aspx" the returned file is automatically downloaded to the deposited download folder.
Is there a opportunity to retrieve the downloaded file or the path to the downloaded file?
The returned Process-object from the Process.Start()-Method doesn't seem to be helpful.
Process.Start does not necessarily download a file. It simply starts a process. You just as easily could start Notepad, which has no side effects (except, of course, what you do with notepad once it's started). If the process you start (in your case, calling a web page) has external effects, the process engine knows nothing about it.
It sounds like you know where the file is supposed to be deposited, so you could use something like a FileSystemWatcher to be alerted when a new file is added (which doesn't necessarily mean it came from your process, though) or get a list of files before and after the process is run.
But there's nothing inherent to System.Process to definitively know what that process did.
Another option might be to call the URL from your code (using WebClient or something similar) and capturing the result (which might be a file, or just a web page, depending on the URL). It's not as general as starting a process and letting the default browser handle the download, but it would capture the results more definitively.
Related
I have an asp.net page that allows a user to select an excel file on his computer and then (1) upload it to the server, and (2) import into a SQL server table. This works fine.
Now I want to enhance the pages functionality to allow the user to select a directory on his computer and then automatically upload/import EACH file (Only of types xls, xlsx) in the clients directory. How can I do this? (Alternatively the user can select a file and then check a checkbox that says "upload all files in this directory", then it will process each file, etc.)
I found a way to loop thru the directory on the server side. But I can't figure it out for client side. Thanks!
You can't. Or at least, you shouldn't be able to.
A properly written browser specifically prevents direct access to the file system of the computer it is running on as a security precaution. The only "correct" way to access a file on a remote machine is to use the browser's File Upload form control on the page and have the user specify the file (or files) they want to send you.
Basically, no.
Many advances have been made in browser security to stop people doing this sort of thing.
The main solution is to use some kind of local plugin that gives this sort of access, but as this can be used for many nefarious deeds, it's very much frowned upon now.
You can of course let the user choose many files, there are various methods for doing this - but you can't automate it by the browser.
What's to stop someone searching for 'creditscards.txt' and uploading it without the user being aware?
One way I can think of is a Java applet. This is executed locally and has full access to the computer and the browser. They are being shut down for similar reason: it's just not safe.
I have outlined a way, but I must emphasize that this must be avoided. It's not because you can that you should.
I have a web form which takes in user information. The value of various text boxes is used to build a html file. I write this html to a file( with specific name) and then prompt user to Save this file.This html is used for creating outlook email signatures. Currently I have this html within the application.This has been deployed to the server. I had to set write permission on this file for all users for it to work.
Are there any security risks? What happens if multiple users access this applications and write to the file at the same time.
When you say the file has "a specific name", do you mean that it is always the same name? If so, then yes, there will be problems if multiple users use this functionality at the same time. They'll be overwriting the one file and downloading each other's data. You would need to generate a unique filename each time the process runs to avoid this.
But do you actually need to save the file?
Or is your goal purely to produce some HTML for the user to download, and the way you are doing this is by writing it to a file, and then prompting them to download that file?
If you don't need to save the file, but rather just need to generate HTML and prompt the user to save, just serve it up as a normal page, and set response headers such that their browser will download it. Something along these lines:
Response.AddHeader("content-disposition", "attachment;filename=my_file.html");
From what I understand, the user fills the web form and submits. Immediately, an html file pops up for download from the server. I think this is very neat implementation of this scenario. You just need to make sure that resources are released properly in order to prevent locking of files.
When multiple users access this application, it should not break since separate files are created with a specific name (as you have mentioned). I don't know what logic has been used to create unique names. At some peculiar situation (this is purely dependent on your name calculation logic) if the calculated specific file name somehow becomes similar to an existing file, you should have code in place to replace or create a different version of the same file name. Locking could occur if you are writing captured data from web form into the same file again and again without disposing your stream/File objects . Make sure you dispose your objects after use in the code.
It would be great if you give access to the application pool of the web application to a user who has write access to that file/folder instead of giving everyone the write access. In this way, your application gets full rights to perform write operations rather than users having rights. If users have write access on the file/folder, it is very easy for anyone to peek in and do something unexpected.
Hope this helps.
I want to download Word file with FTP (thanks to webClient object). It's working fine but not in this case :
Users of my company can open this Word files. So, when a user has opened a file the DownloadData command returns an exception :
error 550 : File unavailable (e.g., file not found, no access).
There is a way to download an opened file with FTP ? If not, what can i do ?
Thanks !
This is almost assuredly a poor architecture for whatever project you are working on. You probably want to separate the user editable document store from the downloadable document store. There are many solutions for keeping the two directories in sync when a change is made by a user (dropbox, mesh, or even custom scripts using robocopy and such).
When an Office program opens a file for modification, it intentionally opens it exclusively. As a result, no FTP server is going to be able to open it, unless it uses the backup API. If it uses the backup API, it may well get some intermediate state, since Office may be in the middle of writing something at any time. So no sane person implements an FTP server to use the backup API.
I am trying to develop a web page which will allow users to download to the Directory of there Choice. In my search to find away to do this, I had found that you can't use "FolderBrowserDialog". I am also finding that the examples online will only search the websrver directeory path and not the local machine. Is there anyway to get the Directory Path? I have the code to FTP the File Down, I just have to replace the code for "FolderBrowserDialog".
This isn't really up to you as a website, it's really the browsers job to store where they want files downloaded to. There is a pretty necessary separation between the website and client machine going on here.
In a web application you can't arbitrarily read or write stuff on the user's computer. That would be a gaping security hole, so it just can't be done. The 'Save As' dialog is something that happens entirely client-side. Your control over the download process ends as soon as you set the MIME type and send the file data away.
Browsers wont usually let you to choose location for individual download. you can change your browser settings to update the download folder. If you want to have this in your webpage, you probably need to have an activex control. this activex control can show the directories in the client machine so that user can select those. You need to write code to download the file to that location using the WebClient.DownloadFile method.
http://msdn.microsoft.com/en-us/library/ez801hhe.aspx
Don't expect this activex control works with all your users.Browsers usually block activex control or prompt the user (if the setting is like that).
EDIT Solution Found: See my post below.
We are writing a library that reads in a TIF file from a scanner. Basically, its a scantron. We are examining the form and reading values from it.
Currently we have a windows test app, and we give it a filepath as a string ("c:\testing\image.tif"). It loads up the tif, reads the document correctly and parses the values.
We also have an ASP.NET web application. We have a test page that does exactly what the windows app does, we hand it an identical string, and i calls the same function on the same class from the same library. It however does NOT read the form correctly. We have verified that it does it fact load up the tif file, and it is actually filled with data (pixels we expect to be white/black are white/black when we examine the Bitmap obect in the immediate window of Visual Studio).
The specific problem is in a library called DataMatrix we use to scan a bar-code off the document. This function is supposed to return a List<string>, each of which is a barcode the library found on the document. In the windows app, this function (DataMatrixDecoder.DecodeBarcode(bitmap)) correctly returns with a Count=1. When using the asp.net app, this returns with Count=0.
Because its the exact same image file, I cannot imagine the problem is in DataMatrix. I can only assume its something with ASP.NET or something.
This isn't even my project, but another guy and I are helping our coworker figure this out, and we are just pulling our hair out. All signs indicate that ASP.NET is correctly loading and handing the image off disk to the "processor" class (which is a class library that uses the DataMatrix stuff, we are not doing ANY code in ASP.NET except for opening/handing the file to the function.).
Does anyone have any ideas as to what it might be, or different things we can check?
I'm not even sure what kind of information to give so I tried to say it all, if you have any questions please ask I'd be more than happy to elaborate on anything. Thanks.
edit:
this is the code on the ascx.cs code-behind, in a button-click event:
if (formReader.ReadTIFF(#"c:\testing\image.tif"))
{
messages.Controls.Add(HtmlHelper.DivSuccess("Read successful."));
}
The formReader class then open the file with a FileStream, and uses that to create a Bitmap. The ASP.NET application is not actually opening the file at all (we were uploading it through a FormUpload control, but after experiencing problems we dummied it down to this). This is the most perplexing thing, that it works in the windows app but not from this web site. ASP.NET has full permissions on that folder to do whatever it wants. It can open the image fine, and the bitmap it creates from the FileStream is the actual image.
edit: Also, the ReadTIFF function right now copies the FileStream into a MemoryStream, ensuring its a not a problem streaming from disk (the entire file is in memory).
How are you passing the filepath to the web application?
It is possible that the function which Decodes might be swallowing some exception.
Use reflector to examine the library (if you have not written it).
I agree. It seems your problem is most probably related to User rights on the directory where you're trying to access the files from. Try giving your Web users the Full access rights on the source directory.
EDIT
Solution Found: The problem was that the open file dialog was changing the CurrentWorkingDirectory. The reason the website never worked, was because the Environment.CurrentDirectory was set incorrectly. When I manually set the CurrentDirectory to the websites' bin folder, parsing works correctly.
Small update. Using the Windows App, and selecting the file via OpenFileDialog, will cause the barcode decoder to fail. Technically, I am using the exact same string to hand to the parser ("c:\testing\image.tif"), yet when I use the OpenFileDialog to get the string, the decoder fails. Is there a clue in this?
update: In fact, even if I don't use the string the OpenFileDialog gives me, if I just open the file dialog at all, it will fail. I don't get this. It's something simple. I need to debug the C++ DataMatrix library, really.