I feel like I've gone in a thousand circles on this and it should be much simpler than it is, but basically I have this simple resume submission page in asp.net using C#. The files are on the server in a folder but it seems every solution I find just does nothing at all when I try to execute it.
Here's some examples of some of the things I've tried:
string folder = "~/App_Data/Resumes/" + Session["ResumeName"].ToString();
string folder2 = Server.MapPath("~/App_Data/Resumes/" + Session["ResumeName"].ToString());
System.Web.VirtualPathUtility.ToAbsolute(folder);
System.Diagnostics.Process.Start(folder2);
I'd never imagined something as simple as opening a document from a server would be so difficult. Is it not possible to just simply have a link to a document and have it opened when the user clicks it? That's all I want to accomplish. Thank you reading my through my frustration.
To clarify, System.Diagnostics.Process.Start(folder2); works locally, but doesn't work on the web server. I want this to work on the web server so that's why that one is out. I've tried to make it possible for the web server to open that file but I don't think I know enough about IIS to make it happen.
App_Data is a protected folder in ASP.NET. IIS will not serve anything from that folder directly to the client-- you will need to build a handler that serves the document directly.
Here are a few samples, though they aren't representative of everything you need to do. Others can feel free to add additional resources. Your handler would have to open the file and write it to the output stream:
Using a c# handler to serve up wav files cuts audio short (only a couple of seconds)
ASP.net cache ASHX file server-side
Don't use folders like App_Data to have your downloadable resources .
There is a similar question on stackoverflow which will give you a exact step by step answer - ASP.NET file download from server
From the title of your question, If I understand correctly you want to open a .doc or .pdf on a link or button click. This will open the respective document on the client.
<a id="lnkResume" href="#" runat="server" target="_blank">Download Resume</a>
And you can set the dynamic resume name from the code like below
string fileName = Session["ResumeName"].ToString();
lnkResume.HRef = Server.MapPath("~/App_Data/Resumes/" + fileName);
Related
I have a server where word files are stored, i want by pressing a link the user can open the word file from the server, edit in it then saving it back to the server.
till now i figured out that i cant do so directly, but to save the file locally , edit on it then upload it back again.
so is there is a better way to do so? if not how to wait till the file is saved then automatically be uploaded again .
I think your best bet is probably to look into some of the controls out there you can buy to do this. Not necessarily cheap but depends on how much you need this functionality:
https://www.syncfusion.com/aspnet-mvc-ui-controls/word-processor
Regarding detecting a file change locally... you can use the FileSystemWatcher to do this on a folder... this isn't something that will work inherently from a website though. YOu would need to implement and distribute a windows service that your users would install and they would need to download the file they were changing into the folder(s) that your FileSystemWatcher was watching...
https://learn.microsoft.com/en-us/dotnet/api/system.io.filesystemwatcher?view=netframework-4.8
https://learn.microsoft.com/en-us/dotnet/framework/windows-services/walkthrough-creating-a-windows-service-application-in-the-component-designer
Hope this helps.
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.
So I'm tasked to build a file upload for our application, which is using Visual WebGUI and therefore mostly looks like WinForms codewise.
The problem is, I don't have any clues as to where to start. I tried looking through our Download class, but it just takes a file and puts it into the response.
I tried google, but there's nothing on uploading something.
I don't understand enough of how websites work to even ask myself or google the right question. I have no Idea how a website tells the browser to get a file. And if I find out how that works, I still need to somehow get VWG to do that. I can't directly interact with the browser (except when I write javascript, but I'm not sure I can get a response from js).
Ideas and clues to where to start would be great too, I just need somewhere to start.
Let me know if you need any more information or clarification, as I'm not sure which kind of information you need for this.
Visual WebGUI has a built in Upload mechanism, called the UploadControl.
Since you're using VWG, you should check out the Companion Kit which is one of few remaining resources out there for Visual Web Gui. It gives an example of the upload control. It also gives example code, which you can download.
In short, what happens is that VWG will handle the JS components of getting the file. You don't have to worry about JavaScript, that's the point of VWG. In C#, you'll code the UploadControl, and what you "get" is the information about the file like Name, Size, MIME type, etc. Refer to the companion kit for info on this.
Steps:
1) Add the UploadControl to a form
this.mobjUploadControl = new Gizmox.WebGUI.Forms.UploadControl();
2) Wire up the UploadControl
this.mobjUploadControl.UploadFileCompleted += new Gizmox.WebGUI.Forms.UploadFileCompletedHandler(this.mobjUploadControl_UploadFileCompleted);
3) Handle the actual upload.
private void mobjUploadControl_UploadFileCompleted(object sender, UploadCompletedEventArgs e)
{
UploadFileResult uploadedFile = e.Result;
// binary data for file, can be used to store to filesystem, db, etc
byte[] fileData = File.ReadAllBytes(uploadedFile.TempFileFullName);
// filename of what was uploaded
string fileName = uploadedFile.Name;
}
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.