Link to home
Start Free TrialLog in
Avatar of LTurk
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.  
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

ASKER CERTIFIED SOLUTION
Avatar of Greg Wright
Greg Wright
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of LATurk
LATurk

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

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

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.
Avatar of LTurk

ASKER

I figured it out.  I was logging in using the wrong user name.  Thanks!