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

Open in new window

LTurkAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Greg WrightSr. Database AdministratorCommented:
Unfortunately, this isn't possible. This kind of operation is a security risk and is not permitted. It must be transferred using a medium like the buffer (http), or webservice, etc. But then must be "actively" saved to the client computer by the user.

The ASHX file is a server side file, and thus, is preparing information to be transferred to the client, therefore, the file can't be written there first. It must be retrieved from the database, written to the server buffer, sent to the client, then saved. No other option is available unless you have a client side application with permissions by the user to install, transfer, and process this way. Then, the installed application takes care of this entire process (like the Windows Update mechanisms).
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
LATurkCommented:
If the client side application has permission to do this, how would I tell it to write the file to the client computer.

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.pdf",FileMode.Create,FileAccess.Write);

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.  
            cmd = db.GetStoredProcCommand("spFaxViewer");
            db.DiscoverParameters(cmd);
            cmd.Parameters["@faxId"].Value = Session["faxId"].ToString();
            dr = db.ExecuteReader(cmd);
            while (dr.Read())
            {
            FileStream fs = new FileStream("C:\\temp\\temp.pdf",FileMode.Create,FileAccess.Write);
            BinaryWriter writer = new BinaryWriter(fs);
            byte[] ba = new byte[(int)(dr["fileSize"])];

            writer.Write((byte[])(dr["image"]));
            writer.Close();
            fs.Close();
            }
            dr.Close();

Open in new window

0
Greg WrightSr. Database AdministratorCommented:
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\application1\procedureashx\procfiles\

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

0
LATurkCommented:
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.
0
LTurkAuthor Commented:
I figured it out.  I was logging in using the wrong user name.  Thanks!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.