文件下载客户端无法正常工作

时间:2017-07-31 14:52:55

标签: javascript c# asp.net asp.net-web-api

我正在尝试从ASP.Net Web API下载文件。我创建了一个包含文件内容的内存流,之后我将内存流分配给响应,如下所示:

    var stream = new MemoryStream();

    if(document.documentSelection == "Test Case Specification")
    {
        stream = testSpecificationDoc(document);
    }

    HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
    result.Content = new StreamContent(stream);
    result.Content.Headers.ContentType =
    new MediaTypeHeaderValue("application/octet-stream");
    return result;

结果,我得到了这个:

Response {url: "http://localhost:16339/api/Document/Create", ok: true, status: 200, statusText: "OK", headers: Headers…}
body:""
bodyText:""
headers:Headers
ok:true
status:200
statusText:"OK"
url:"http://localhost:16339/api/Document/Create"
data:(...)
__proto__:Object

完整代码:

public HttpResponseMessage Create([FromBody]AppDocument document)
{
    if (!ModelState.IsValid)
    {
        //return BadRequest(ModelState);
    }

    var stream = new MemoryStream();

    if(document.documentSelection == "Test Case Specification")
    {
        stream = testSpecificationDoc(document);
    }

    HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
    result.Content = new StreamContent(stream);
    result.Content.Headers.ContentType =
    new MediaTypeHeaderValue("application/octet-stream");
    return result;
}


private MemoryStream testSpecificationDoc(AppDocument document)
{
    String disclaimerTxt = "The contents of this document are under copyright of Critical Manufacturing S.A. it is released on condition that it shall not be copied in whole, in part or otherwise reproduced (whether by photographic, or any other method) and the contents therefore shall not be divulged to any person other than that of the addressee (save to other authorized offices of his organization having need to know such contents, for the purpose for which disclosure is made) without prior written consent of submitting company.";
    String date = "DD Month YYYY";

    // document permission title
    DocRun accessTypeTitle  = new DocRun();
    Run permissionTitle = accessTypeTitle.createParagraph("DOCUMENT ACCESS", PARAGRAPHCOLOR,SMALLFONTSIZE,DEFAULTFONT);

    // document permission (ex: public, internal use only), defined on template side
    DocRun accessTypePermission = new DocRun();
    Run permission = accessTypePermission.createParagraph(document.documentAccess, PARAGRAPHTITLECOLOR, SMALLFONTSIZE, DEFAULTFONT);

    DocRun disclaimerTitle = new DocRun();
    Run disclaimerTitleTxt = disclaimerTitle.createParagraph("DISCLAIMER", PARAGRAPHCOLOR, SMALLFONTSIZE, DEFAULTFONT);

    DocRun disclaimerDescription = new DocRun();
    Run disclaimerDescriptionTxt = disclaimerDescription.createParagraph(disclaimerTxt, PARAGRAPHTITLECOLOR, SMALLFONTSIZE, DEFAULTFONT);

    DocRun documentType = new DocRun();
    Run documentTypeTxt = disclaimerDescription.createParagraph(document.documentSelection, TITLECOLOR, DOCTITLE, FONTTYPETITLE);

    DocRun projectNameTitle = new DocRun();
    Run projectNameTxt = disclaimerDescription.createParagraph(document.projectName, SUBTITLECOLOR, FONTSIZESUBTITLE,FONTTYPE);

    DocRun dateParagraph = new DocRun();
    Run dateTxt = disclaimerDescription.createParagraph(date, PARAGRAPHTITLECOLOR, DATEFONTSIZE, DEFAULTFONT);

    /*Doc simpleDoc = new Doc();
    simpleDoc.CreateDoc(dateTxt, projectNameTxt);*/

    var stream = new MemoryStream();
    using (WordprocessingDocument doc = WordprocessingDocument.Create(stream, WordprocessingDocumentType.Document, true))
    {
        MainDocumentPart mainPart = doc.AddMainDocumentPart();

        String clientLogoPath = getCustomerLogo(document.projectName);

        PageMargin pageMargins = new PageMargin();
        pageMargins.Left = MathOpenXml.convertUInt32ValueToDxa(DEFAULTLEFTMARGIN);
        pageMargins.Right = MathOpenXml.convertUInt32ValueToDxa(DEFAULTRIGHTMARGIN);
        pageMargins.Bottom = MathOpenXml.convertInt32ValueToDxa(DEFAULTBOTTOMMARGIN);
        pageMargins.Top = MathOpenXml.convertInt32ValueToDxa(DEFAULTTOPTMARGIN);

        //Important needed to access properties (sections) to set values for all elements.
        SectionProperties sectionProps = new SectionProperties();
        sectionProps.Append(pageMargins);

        // Logo company construction
        DocImage companyLogo = new DocImage();
        Run imageLogo = companyLogo.imageCreator(mainPart,COMPANYLOGOPATH,COMPANYIMAGENAME,COMPANYLOGOWIDTH,COMPANYLOGOHEIGHT,COMPANYIMAGEALING,null,null,"Square");

        DocImage titleShape = new DocImage();
        Run imageShape = titleShape.imageCreator(mainPart, SHAPEIMAGEPATH, TITLESHAPEIMAGENAME, TITLESHAPEWIDTH, TITLESHAPEHEIGHT, SHAPEIMAGEALING,null,0.24,"Behind Text");

        DocImage clientImage = new DocImage();
        Run clientLogo = clientImage.imageCreatorUrl(mainPart, SHAPEIMAGEPATH, TITLESHAPEIMAGENAME, TITLESHAPEWIDTHCLIENTLOGO, TITLESHAPEHEIGHTCLIENTLOGO, CLIENTIMAGEALIGN,clientLogoPath,1.1,null, "Top and bottom");

        new Document(new Body()).Save(mainPart);

        Body body = mainPart.Document.Body;

        body.Append(sectionProps);

        body.Append(new Paragraph(new ParagraphProperties(new Indentation() { Start = MathOpenXml.convertUInt32ValueToDxa(DEFAULTLEFTIDENT).ToString() }),
            new Run(imageLogo)));

        body.Append(new Paragraph(
            new Run(
                new Run(new Break()))));

        body.Append(new Paragraph(
            new Run(
                new Run(new Break()))));

        body.Append(new Paragraph(
            new Run(
                new Run(new Break()))));

        body.Append(new Paragraph(
            new Run(
                 new Run(new Break()))));

        body.Append(new Paragraph(
            new Run(imageShape)));

        body.Append(new Paragraph(new ParagraphProperties(new Indentation() { Start = MathOpenXml.convertUInt32ValueToDxa(DEFAULTLEFTIDENT).ToString() }),
            new Run(documentTypeTxt)));
        body.Append(new Paragraph(
            new Run(
                new Run(new Break()))));

        body.Append(new Paragraph(new ParagraphProperties(new Indentation() { Start = MathOpenXml.convertUInt32ValueToDxa(DEFAULTLEFTIDENT).ToString() }),
            new Run(projectNameTxt)));

        body.Append(new Paragraph(new ParagraphProperties(new Indentation() { Start = MathOpenXml.convertUInt32ValueToDxa(DEFAULTLEFTIDENT).ToString() }),
            new Run(dateTxt)));

        body.Append(new Paragraph(        
            new Run(
                 new Run(new Break()))));

        body.Append(new Paragraph(
            new Run(
                new Run(new Break()))));


        body.Append(new Paragraph(new ParagraphProperties(new Indentation() { Start = MathOpenXml.convertUInt32ValueToDxa(DEFAULTLEFTIDENT).ToString() }),
            new Run(clientLogo)));

        body.Append(new Paragraph(
            new Run(
                new Run(new Break()))));
        body.Append(new Paragraph(new ParagraphProperties(new Indentation() { Start = MathOpenXml.convertUInt32ValueToDxa(DEFAULTLEFTIDENT).ToString() }, new SpacingBetweenLines() { Line = "350", LineRule = LineSpacingRuleValues.Auto, Before = "0", After = "0" })));

        body.Append(new Paragraph(new ParagraphProperties(new Indentation() { Start = MathOpenXml.convertUInt32ValueToDxa(DEFAULTLEFTIDENT).ToString() }, new SpacingBetweenLines() { Line = "350", LineRule = LineSpacingRuleValues.Auto, Before = "0", After = "0" }),
            new Run(permissionTitle)));

        body.Append(new Paragraph(new ParagraphProperties(new Indentation() { Start = MathOpenXml.convertUInt32ValueToDxa(DEFAULTLEFTIDENT).ToString() }, new SpacingBetweenLines() { Line = "700", LineRule = LineSpacingRuleValues.Auto, Before = "0", After = "0" }),
            new Run(permission)));



        body.Append(new Paragraph(new ParagraphProperties(new Indentation() { Start = MathOpenXml.convertUInt32ValueToDxa(DEFAULTLEFTIDENT).ToString() }, new SpacingBetweenLines() { Line = "350", LineRule = LineSpacingRuleValues.Auto, Before = "0", After = "0" }),
            new Run(disclaimerTitleTxt)));

        body.Append(new Paragraph(new ParagraphProperties(new Indentation() { Start = MathOpenXml.convertUInt32ValueToDxa(DEFAULTLEFTIDENT).ToString() }),
            new Run(disclaimerDescriptionTxt)));

        mainPart.Document.Save();

    }

    stream.Seek(0, SeekOrigin.Begin);
    Directory.CreateDirectory(HostingEnvironment.MapPath(DOCUMENTSLOCATION));
    System.IO.File.WriteAllBytes(HostingEnvironment.MapPath("~/Files/test5.docx"), stream.ToArray());
    return stream;
}

现在,我需要提供一个下载选项,客户端。我怎么能这样做?
我是用ASP.Net Web Api做的吗?

0 个答案:

没有答案
相关问题