Read file without exclusive lock - asp.net

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?

Related

Determining .exe file in time of upload

I have developed File Upload web page in ASP.NET. Now user can rename a .exe file to txt or some other extension and upload the same. I want to restrict that. How I can implement that in ASP.NET?
The only safe way to do this is to get the byte [] from the file that has been posted and examine it to determine if the file is indeed in one of the formats you allow the user to upload. You don't need to save the file, you can just get the byte[] from the HttpPostedFile object.
Other than examining the content (looking for magic numbers, for example) there isn't an infallible way to make sure that the user is not attempting to upload something that you don't allow.

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.

Aspx file generation

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);

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.

WMP: streaming, then saving

I use the Windows Media Player Control (WMP.DLL) in my application to download (stream) and show videos. Once the video file is fully downloaded, I would like to save it to disk. Unfortunately I can't find a way to do so. I thought of using a hack: probably WMP does not hold the whole file in memory, so it must be somewhere on disk as temp file - but where?
So, in short: does anybody know how to tell WMP control to save a streamed video to disk? (The Windows Media Player knows how to to it, it has a 'Save as' command, so there must be a way to do it.)
Thanks,
Jo
A workaround might be to check the temporary internet files folder - the file might be there.
That did it, thanks! I don't even have to search for the files manually , I can use URLDownloadToFile. If Windows finds the file in Temporary Internet Files, it just takes it from there instead of downloading it again. Problem solved!

Resources