[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Can I force ashx.cs page to binarywrite to local computer instead of displaying in browser.

Posted on 2010-03-30
5
Medium Priority
?
367 Views
Last Modified: 2012-05-09
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

0
Comment
Question by:LTurk
  • 2
  • 2
5 Comments
 
LVL 2

Accepted Solution

by:
Greg Wright earned 2000 total points
ID: 29165106
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
 

Expert Comment

by:LATurk
ID: 29213602
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
 
LVL 2

Expert Comment

by:Greg Wright
ID: 29240869
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
 

Expert Comment

by:LATurk
ID: 29797209
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
 

Author Closing Comment

by:LTurk
ID: 31709153
I figured it out.  I was logging in using the wrong user name.  Thanks!
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video teaches viewers about errors in exception handling.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

640 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question