Create and download an text file in a client-server system

六月ゝ 毕业季﹏ 提交于 2019-12-11 13:16:05

问题


in ms dynamics crm I will create over an own ribbon button an text file with some contact attributes. This file I would like to save or download to the client machine, so the user can work with it.

How can i do this? I'm going crazy while testing a lot of different ways.

  • create with javascript an string and try to downlaod it with data uri. dosen't work

    var content = "test";

    window.open("data:text/octet-stream," + encodeURIComponent(content));

  • try with silverlight (localy I could create a file) but remote dosen't work

  • next try was to fill/create with javascript an *.aspx file on the server and create a text file. But I don't know if this is working or what can i do otherwise?

Please give me some hints to solve this problem.


回答1:


Ok I solved it.

In crm i integrated javascript code to open an aspx site.

function CreateVCF() 
{
   var entityName = Xrm.Page.data.entity.getEntityName();
   var guid = Xrm.Page.data.entity.getId();
   window.open("http://xxxxx/vcfexport.aspx?guid="+guid+ "&name="+entityName );
}

and there i create a vcard and give it back to download

public partial class vcfexport : System.Web.UI.Page {
    protected void Page_Load(object sender, EventArgs e) {

        //Authenticate using credentials of iis       
        ClientCredentials Credentials = new ClientCredentials();

        Uri OrganizationUri = new Uri("http://server/org/XRMServices/2011/Organization.svc");
        Uri HomeRealmUri = null;

        OrganizationServiceProxy serviceProxy = new OrganizationServiceProxy(OrganizationUri, HomeRealmUri, Credentials, null);

        IOrganizationService service = (IOrganizationService)serviceProxy;

        String guidString = Request.QueryString["guid"];
        String entityName = Request.QueryString["name"];

        if (guidString != null && entityName != null && guidString != "" && entityName != "") {

            ColumnSet columnSet = new ColumnSet(true);
            Guid guid = new Guid(guidString);

            // retrieve the contact dataset
            Entity contactEntity = service.Retrieve(entityName, guid, columnSet);

            // create new vcard
            VCard vcard = new VCard();
            vcard.FirstName = contactEntity.GetAttributeValue<String>("firstname");
            vcard.LastName = contactEntity.GetAttributeValue<String>("lastname");
            //......

            String fileName = vcard.LastName + " " + vcard.FirstName + ".vcf";

            Response.ContentType = "text/x-vcard";
            Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
            // give download window
            Response.Write(vcard.ToString());
            Response.End();


        } else {
            lblError.Text = "Es ist ein Fehler aufgetreten. Entityname oder guid sind falsch";
        }
    }


来源:https://stackoverflow.com/questions/14757189/create-and-download-an-text-file-in-a-client-server-system

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!