Saving rtf file.

Last post 03-31-2009, 6:17 AM by rles. 5 replies.
Sort Posts: Previous Next
  •  06-19-2006, 4:10 PM 20296

    Saving rtf file.

    Hi, I'm trying to get the editor's save button to download a rft file on click.  I have it basically working except for the fact that the saved file does not open properly.  What can I do to make this happen?

    Here's a code snippet:

    private void Editor1_PostBackCommand(object sender, System.Web.UI.WebControls.CommandEventArgs e)
            {
                if(string.Compare(e.CommandName,"Save",true)==0)
                {
                    byte[] output = ConvertStringToByteArray(Editor1.Text);
                    Response.AddHeader("Content-Disposition", "attachment; filename=cute.rtf");
                    Response.ContentType = "application/rtf";
                    Response.Clear();
                    Response.Buffer = true;
                    Response.BinaryWrite(output);
                    Response.End();
                }
            }

            public static byte[] ConvertStringToByteArray(string stringToConvert)
            {
                return (new UnicodeEncoding()).GetBytes(stringToConvert);
            }

    It looks garbled in wordpad and will not open in MS word.  However in notepad it just shows the plain html output string.  Any ideas?  Thanks!
  •  06-19-2006, 4:24 PM 20297 in reply to 20296

    Re: Saving rtf file.

  •  06-19-2006, 4:39 PM 20298 in reply to 20297

    Re: Saving rtf file.

    The example is nice but it doesn't really help me unless I can see the code behind it.  I'm trying to get the file to save directly to the client machine on the save button click rather than having it save it to the server like what is done in the example.  Should I be using another function to get the text out of the editor to put it in rtf format rather than html text?  Say for example instead of using Editor1.Text to get the contents, maybe Editor1.RTFText or something like that?
  •  06-19-2006, 4:41 PM 20299 in reply to 20298

    Re: Saving rtf file.

    The above example is included in the download package.
     
    Here is the code:
     

    <script runat="server">
     void Page_Load(object sender, System.EventArgs e)
      {
         if (IsPostBack)
      {
       Editor1.SaveRTF("document.rtf");
       textbox1.Text = Editor1.Text;
         }
      else
      {
       Editor1.LoadRTF("document.rtf");
      }  
     }
    </script>
     
     
    Does it help?
     
     

    asp.net Chat http://cutesoft.net/ASP.NET+Chat/default.aspx
    Web Messenger: http://cutesoft.net/Web-Messenger/default.aspx
    asp.net wysiwyg editor: http://cutesoft.net/ASP.NET+WYSIWYG+Editor/default.aspx
    asp wysiwyg html editor: http://cutesoft.net/ASP
    asp.net Image Gallery: http://cutesoft.net/ASP.NET+Image+Gallery/default.aspx
    Live Support: http://cutesoft.net/live-support/default.aspx

  •  06-20-2006, 9:44 AM 20329 in reply to 20299

    Re: Saving rtf file.

    I got it working.  I had to save the file on the server first, then initiate the download to the client machine.  If anyone is interested, it looks like this (I haven't gotten to deleting the temp file yet):

    private void Editor1_PostBackCommand(object sender, System.Web.UI.WebControls.CommandEventArgs e)
      {
       if(string.Compare(e.CommandName,"Save",true)==0)
       {
        Random rand = new Random();
        int randnum = rand.Next();
        String fileName;
        fileName = "PUT FILE PATH HERE" + randnum.ToString() + ".rtf";
        Editor1.SaveRTF(fileName);
        FileInfo fInfo = new FileInfo(fileName);
        long numBytes = fInfo.Length;
        FileStream fStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
        BinaryReader br = new BinaryReader(fStream);
        byte [] data = br.ReadBytes((int)numBytes);
        Response.AddHeader("Content-Disposition", "attachment; filename=cute.rtf");
        Response.ContentType = "application/rtf";
        Response.Clear();
        Response.Buffer = true;
        Response.BinaryWrite(data);
        Response.End();
        br.Close();
        fStream.Close();
       }
      }
  •  03-31-2009, 6:17 AM 50510 in reply to 20329

    Re: Saving rtf file.

    hi,

     
    Where do I put this code? 
     

View as RSS news feed in XML