DocX with filestream doesn't save

主宰稳场 提交于 2019-12-24 19:28:29

问题


So for a project I am creating Résumé's and i need to save them into a docx file. It is a ASP.NET MVC application and for the docx generating I'm using the libray docx to create the document.

I can create the file but the filestream doesn't add the content i put into it.

Here is a the code i use

public ActionResult CVToWord(int id) 
        {
            var CV = CVDAO.CV.Single(cv => cv.id == id);
            var filename = "CV - " + CV.firstname + " " + CV.name + " - " + CV.creationdate.ToString("dd MM yyyy") + ".docx";
            System.IO.FileStream stream = new System.IO.FileStream(filename, System.IO.FileMode.OpenOrCreate);

            using (DocX doc = DocX.Create(stream)) 
            {
                Paragraph title = doc.InsertParagraph();
                title.Append(CV.firstname + " " + CV.name);
                doc.Save();
            }

            return File(stream, "application/vnd.openxmlformats-officedocument.wordprocessingml.document", filename);
        }

Like i said, this creates the file but the file doesn't have any content. Any one have a idea why?


回答1:


public ActionResult CVToWord(int id) 
        {
            var CV = CVDAO.CV.Single(cv => cv.id == id);
            var filename = "CV - " + CV.firstname + " " + CV.name + " - " + CV.creationdate.ToString("dd MM yyyy") + ".docx";

            using (DocX doc = DocX.Create(filename)) 
            {
                Paragraph title = doc.InsertParagraph();
                title.Append(CV.firstname + " " + CV.name);
                doc.Save();
            }

            System.IO.FileStream stream = new System.IO.FileStream(filename, System.IO.FileMode.Open);
            return File(stream, "application/vnd.openxmlformats-officedocument.wordprocessingml.document", filename);
        }

This works




回答2:


I think you need to close your FileStream:

stream.Close();



回答3:


I'd write a custom ActionResult for this:

public class CVResult: ActionResult
{
    private readonly CV _cv;

    public CVResult(CV cv)
    {
        _cv = cv;
    }

    public override void ExecuteResult(ControllerContext context)
    {
        var response = context.HttpContext.Response;
        response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
        var filename = "CV - " + _cv.firstname + " " + _cv.name + " - " + _cv.creationdate.ToString("dd MM yyyy") + ".docx";

        var cd = new ContentDisposition
        {
            Inline = false,
            FileName = filename
        };

        using (var doc = DocX.Create(response.OutputStream))
        {
            Paragraph title = doc.InsertParagraph();
            title.Append(_cv.firstname + " " + _cv.name);
            doc.Save();
        }
    }
}

and now your action result is no longer cluttered with plumbing code:

public ActionResult CVToWord(int id) 
{
    var cv = CVDAO.CV.Single(cv => cv.id == id);
    return new CVResult(cv);
}


来源:https://stackoverflow.com/questions/7634205/docx-with-filestream-doesnt-save

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