Aspx file generation - asp.net

I am working on an .aspx page.its to download a pdf that is generated in the aspx page.
.but when hosted in Amazon cloud i am getting the message.
"The process cannot access the file because it is being used by another process".
but on the subsequent invocation of the .aspx page i am getting the pdf .
The pdf file is being generated.

Asp.Net uses separeted threads for each request. Probably you use some shared resources to generate pdf and don't clean up them. Therefore parellel requests may fail.
Using block (or calling Dispose() directly) may help.
using (StreamReader reader = new StreamReader(#"C:\My Files\test.txt"))
{
..
}
Also make sure you don't open files with exclusive permissions like this:
FileStream fileStream = new FileStream("test.txt", FileMode.Open, FileAccess.ReadWrite, FileShare.None);

Related

how to create a word file on clients from aspx site

my code generates a word document from some data and opens it directly.
when i start it local it works. but when i put the code in the server and try to generate it there it doesnt work.
the file should generate in the clients not on the server. is this possible?
this is how i create the word file:
Dim oApp As Word.Application
Dim oDoc As Word.Document
oApp = CreateObject("Word.Application")
oDoc = oApp.Documents.Add
afte i create the file i can open it with this command:
oApp.Visible = True
Your code is running under IIS on the server. When you run the code locally, it appears to work because your machine is the server and so, when Word opens, you see the window appear. Your code is still running on the "server," though!
You can't open a document directly on a client like this, and nor will the MSWord interop classes you've used affect a client machine. What you could do instead is create the document on the server, and then offer it as a download to your client. Their browser would then offer to the user the typical example of "Open/Save/Cancel" and handle the file as per any other download.
If you can guarantee the presence of a plugin on the client's browser, such that the document can be made to appear in-browser, this would also be an option - the mechanism for serving the file up would be broadly the same, though.

Download and run file in client machine using asp.net

I'm trying to download and run a file to the client machine. The client is aware of that.
It's a ttkgp file that's dynamicly generated.
I've tried using Processs.Start() that worked fine on my local machine (first saved the file to C:\ then lunched it), but it's not working from the server. It's not my server but a hosted one. They are trying to help but no luck so far.
I've seen this code:
public void ProcessRequest(HttpContext context)
{
string fileName = context.Request.QueryString["filename"];
FileInfo fi = new FileInfo(fileName);
context.Response.ContentType = "application/x-rar-compressed";
context.Response.AppendHeader("Content-Disposition",
string.Format("attachment; filename=download{0}", fi.Name));
context.Response.WriteFile(fileName);
context.Response.End();
}
But since I dont know what's "HttpContext context" is, I've no idea if it works.
Is it some server previlges need to be changed? or simply this code will do the trick?
Thank you
UPDATE (24.6.12): I'm nearly finished with the problem, all I need now is to know how to open an html page in a new tab / window and close it second later. Once I'm done, I'll post back here all the process, I'm sure it'll help other people.
UPDATE (26.6.12):
Here's what I've got:
The goal is to download an TTKGP file from asp.net webiste to local user machine and run it.
Step 1: generate the file with code behaind (c#) on the server (V)
Step 2: copy the file or it's content to user machine (X)
Step 3: run the file using JS (V)
Here's the thing: I CAN copy from a text file on the server to a text file on the user machine, but not from TTKGP files. It's strange because this are just text files just a different extantion.
The code for copying text files:
enter code here
function copyremotetxt() // works
{
// copy the txt file
var fso = new ActiveXObject("Scripting.FileSystemObject");
var newfile = fso.CopyFile("remote.txt", "C:\\Users\\***\\local.txt");
}
Perhaps I can change the file type on the user machine?
Notice 1: I know that's a security issue, the site is just for known user not the open public
Notice 2: I know there are better ways to get the task done, but there are strict limitaions on many things
Thanks for those how can help!!
This code will do the trick. It will prompt the client to download and save the file on his computer at the location he decides. What happens next with this file is the client's decision, not yours. He might simply close the Save As dialog, interrupt the download, delete the file, ... It's up to him.
Another remark: this code is extremely dangerous because it allows the client to pass any filename he wants as query string parameter and download it. So he could read absolutely all files on the server which is probably not something that you want to happen.
Ok, this need a different aproach.
I'll try using JavaScript do read the file on the server, rewrite it in the user machine and activate it. Any clues would be grate! For a start, how to I read file in JS? I'm new to it.

Read file without exclusive lock

What is the best way to read image when simultaneous requests try to access the same image file?
Basically I have created tiff viewer in asp.net. It displays thumbnails of all tiff pages and upon click loads full image below. But when I try to read big tiff file (40+ MB), some thumbnails shows up and some throw access denied error. I hope I am clear enough to explain the problem.
you should open the Stream in this way:
using (Stream s = new FileStream(fullFilePath,
FileMode.Open,
FileAccess.Read,
FileShare.ReadWrite))
{
// here use the stream s
}
in this way you open for read and still other processes will be able to read the file as well and only one process, not yours, could eventually acquire the write rights.
see here as well: How do I open an already opened file with a .net StreamReader?

How to create a file on server and save it on server

I m creating a pdf file on the server using iTextSharp.
It is working fine prompting me to save everything is fine.
I have 2 scenario
1) I want to save this file on a location on server
2) I want to save this file in database
My Last line after finishing the code is
document.Close();
So can anyone guide me how can I do it
The following method will create a new file on the disk with a paragraph in it. You could easily modify it to your needs.
Before this will work you must ensue the directory you wish to save to has the correct permissions set. Whatever user account the Application Pool for the website is using will need write access to the folder. Set this in Windows Explorer, Right Click > Properties > Security Tab. This is well documented on the internet.
You can store the filename in the database in many different ways and then link to the file from your UI. I suggest starting with a simple ADO.Net tutorial
protected void CreateDoc(string filename)
{
Document document = new Document();
PdfWriter.GetInstance(document, new FileStream(filename, FileMode.Create));
document.Open();
document.Add(new Paragraph("Hello World!"));
document.Close();
}

Deleting Application Temp File from ASP.Net

I have a WebPage where I am giving the option to to Export the Form data to PDF. I am creating the PDF at run time and store the PDF in a "PDF" folder which is under my application directory. After creating the PDF with the SessionID name I Call following function to show the PDF file in the new browser window:
ResponseHelper.Redirect(Response, "~/PDF/" + Session.SessionID + ".pdf", "_Blank", "");
This PDF contains the private information related to the logged in user. Therefore, I want a way to delete this PDF file once it is shown in the browser to the user. This is because the IIS server allows whole development team to view this folder which is a security risk, and we can't disallow user to view this folder on the server.
Therefore, if I could delete this file as soon as it is loaded in the browser could be a solution of this security risk.
Can anyone suggest some better ways of deleting this file as soon as possbile from the application?
Thanks,
Praveen
what i guess is you are creating PDF file on runtime using Itext and then you save that PDF file in temp directory to show it to user... why don't you use
Response.WriteFile(PDFFILE);
this will write the whole file on the stream without saving it in temp folder.
One way is to write an ashx handler which streams the pdf to the browser, then deletes it when done.
Another, and much better way, is to simply build the PDF in memory (NOT using session) and stream it as soon as it's ready.
UPDATE
I'm doing this with a slightly modified version of iTextSharp. Basically, iTextSharp performed all of it's operations in memory, then saved the file to disk. I changed this to return the memory stream. All the code is already there, it was really just a line or two that had to change.
Then, I used a response.binarywrite to push the stream directly to the browser. viola! no files on disk.
An ashx handler is just like an aspx page, only it has one entry point and doesn't do all of the page processing garbage. It's light weight and communicates back to the browser by response.write calls.

Resources