LTurk
asked on
Can I force ashx.cs page to binarywrite to local computer instead of displaying in browser.
I call an ashx page from an aspx page.
Is it possible to force the file to binarywrite to a local file on the users computer and then I could open it from there as opposed to having the file sent to the browser first.
The below is the code in my ashx page and it does work fine and sends the file to the browser. But sometimes the users have to manipulate the pdf and save it back to the database.
I would like to be able to save the file to a file on the users computer first.
Is it possible to force the file to binarywrite to a local file on the users computer and then I could open it from there as opposed to having the file sent to the browser first.
The below is the code in my ashx page and it does work fine and sends the file to the browser. But sometimes the users have to manipulate the pdf and save it back to the database.
I would like to be able to save the file to a file on the users computer first.
string id = ctx.Request.QueryString["id"];
SqlConnection con = new SqlConnection("Data Source=myDataSource;Initial Catalog=myDatabase;UID=myUser; Password=myPassword");
SqlCommand cmd = new SqlCommand("SELECT tblFax.image FROM tblFax WHERE faxId = " + id, con);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@faxId", id);
ctx.Response.ContentType = HttpContext.Current.Session["fileExtension"].ToString();
ctx.Response.AddHeader("content-disposition", "attachment;filename=myFax.pdf");
con.Open();
byte[] pict = (byte[])cmd.ExecuteScalar();
ctx.Response.BinaryWrite(pict);
con.Close();
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
To answer your first question, you have to create a C# application/library which is installed on the client computer, with their permission. Then you communicate with the server to transmit the file and save it there and display it to the user.
The next answer is ...
I would suggest that you create a file under your website path relative to your ashx file. For example: c:\projectswww\application 1\procedur eashx\proc files\
Give this directory the permissions to IUSR to write files and get the Server.MapPath("procfiles\ " & strUniqueFilename). This file will then be written out using the BinaryWriter to the http stream.
Set FileStream = CreateObject("ADODB.Stream ")
FileStream.Charset = "UTF-8"
FileStream.Type = 2
FileStream.Open
FileStream.WriteText strFileContentsToWriteOut
FileStream.SaveToFile (sFilePath )
Open the file for reading and send it to the user on the Response.
Reponse.WriteBinary()
The next answer is ...
I would suggest that you create a file under your website path relative to your ashx file. For example: c:\projectswww\application
Give this directory the permissions to IUSR to write files and get the Server.MapPath("procfiles\
Set FileStream = CreateObject("ADODB.Stream
FileStream.Charset = "UTF-8"
FileStream.Type = 2
FileStream.Open
FileStream.WriteText strFileContentsToWriteOut
FileStream.SaveToFile (sFilePath )
Open the file for reading and send it to the user on the Response.
Reponse.WriteBinary()
I want to award the points to snapjaq.
I'm not sure what I'm doing wrong, but I don't see the option to Accept his Answer. All I see is the "Was this comment helpful?" option.
I'm not sure what I'm doing wrong, but I don't see the option to Accept his Answer. All I see is the "Was this comment helpful?" option.
ASKER
I figured it out. I was logging in using the wrong user name. Thanks!
I tried using the code below in the aspx page and it does save the file to my C:/temp folder, but I have a feeling when I publish it, it will save the file to the C:/temp folder on the server. How would I change the file path in the below line?
FileStream fs = new FileStream("C:\\temp\\temp
Or, do you think it would be better to let the file go to the C drive on the server and have the user manipulate from there. I guess I would just have to be sure each file would always have a unique name since there will be multiple users doing this at the same time.
The application I am writing is only going to be used inside the office.
Open in new window