locked
Download File from CRM Database RRS feed

  • Question

  • Hi Guys,

    I want to make a link where user can download file by clicking the link. THe file name will become the link. The file is in CRM database where the file is stored in annotation entity.

    The following is the code to attach file to annotation entity :

     Byte[] data;
     ASCIIEncoding encode = new ASCIIEncoding();
    data = encode.GetBytes(str.ToString());
    string b64 = Convert.ToBase64String(data);

    annotation note = new annotation();           
    note.documentbody = b64;

    My question is how to download the file from CRM database? We can retrieve the value of the documentbody(attribute of Annotation entity) by code. Then how do I make it being able to be downloaded? 

    Thank you for your help


    Regards, Dedy
    Friday, April 16, 2010 10:53 AM

Answers

  • You can use File Stream to write the file to the appropriate downloadable path.

    refer http://msdn.microsoft.com/en-us/library/cc151179.aspx

    • Marked as answer by Dedy Hartono Monday, April 19, 2010 4:46 AM
    Friday, April 16, 2010 11:00 AM
  • Use the HttpResponse object in your ASP.NET app.

    this is how to putput a word document so the user receives a Open, Save, Cancel dialog.

    Response.ClearContent();
    Response.ClearHeaders();
    Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
    Response.AddHeader("content-disposition", "attachment; filename=" + FileName);
    Response.OutputStream.Write(Stream.ToArray(), 0, Stream.ToArray().Length);
    Response.Flush();
    Response.Close();

     

    The stream in this example is a MemoryStream of the document.

     


    MSCRM Bing'd - http://bingsoft.wordpress.com
    Friday, April 16, 2010 11:01 AM
    Moderator
  • Hi Guys,

    I get the solution. :)

    The string must be converted back by using : Convert.FromBase64String(string);

    Okay. Thank you guys.


    Regards, Dedy
    • Marked as answer by Dedy Hartono Monday, April 19, 2010 4:46 AM
    Monday, April 19, 2010 4:44 AM

All replies

  • You can use File Stream to write the file to the appropriate downloadable path.

    refer http://msdn.microsoft.com/en-us/library/cc151179.aspx

    • Marked as answer by Dedy Hartono Monday, April 19, 2010 4:46 AM
    Friday, April 16, 2010 11:00 AM
  • Use the HttpResponse object in your ASP.NET app.

    this is how to putput a word document so the user receives a Open, Save, Cancel dialog.

    Response.ClearContent();
    Response.ClearHeaders();
    Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
    Response.AddHeader("content-disposition", "attachment; filename=" + FileName);
    Response.OutputStream.Write(Stream.ToArray(), 0, Stream.ToArray().Length);
    Response.Flush();
    Response.Close();

     

    The stream in this example is a MemoryStream of the document.

     


    MSCRM Bing'd - http://bingsoft.wordpress.com
    Friday, April 16, 2010 11:01 AM
    Moderator
  • Hi Guys,

    Thank you for your answer. Both of your answer help me a lot.

    I can already download the file from CRM database. Unfortunately, I cann't open the file after downloading it.
    I believe this problem is caused by "Convert.ToBase64String(data);"

    When we save the file into annotation, the workflow process to save a file are :
    GetTheBufferOfFile -> convertTheBufferToBase64String ->saveTheStringIntoCrmDatabase. 

    When I force to open the file, the content of file is the string of the converting(Convert.ToBase64String). How do I get the original content of file? Thanks again for your help guys.


    Regards, Dedy
    Monday, April 19, 2010 3:14 AM
  • Hi Guys,

    I get the solution. :)

    The string must be converted back by using : Convert.FromBase64String(string);

    Okay. Thank you guys.


    Regards, Dedy
    • Marked as answer by Dedy Hartono Monday, April 19, 2010 4:46 AM
    Monday, April 19, 2010 4:44 AM