小编MrM*_*MrM的帖子

如何返回MemoryStream docx文件MVC?

我有一个docx文件,我想在编辑后返回.我有以下代码......

object useFile = Server.MapPath("~/Documents/File.docx");
object saveFile = Server.MapPath("~/Documents/savedFile.docx");
MemoryStream newDoc = repo.ChangeFile(useFile, saveFile);
return File(newDoc.GetBuffer().ToArray(), "application/docx", Server.UrlEncode("NewFile.docx"));
Run Code Online (Sandbox Code Playgroud)

该文件似乎很好,但我收到错误消息("文件已损坏"和另一个说"Word发现不可读的内容.如果您信任源点击是").有任何想法吗?

提前致谢

编辑

这是我模型中的ChangeFile ......

    public MemoryStream ChangeFile(object useFile, object saveFile)
    {
        byte[] byteArray = File.ReadAllBytes(useFile.ToString());
        using (MemoryStream ms = new MemoryStream())
        {
            ms.Write(byteArray, 0, (int)byteArray.Length);
            using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(ms, true))
            {                    
                string documentText;
                using (StreamReader reader = new StreamReader(wordDoc.MainDocumentPart.GetStream()))
                {
                    documentText = reader.ReadToEnd();
                }

                documentText = documentText.Replace("##date##", DateTime.Today.ToShortDateString());
                using (StreamWriter writer = new StreamWriter(wordDoc.MainDocumentPart.GetStream(FileMode.Create)))
                {
                    writer.Write(documentText);
                }
            }
            File.WriteAllBytes(saveFile.ToString(), ms.ToArray()); …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-mvc file-upload docx asp.net-mvc-3

10
推荐指数
2
解决办法
2万
查看次数

我可以有这样的if语句吗?如果Test ="test1"或"test2"或"test3"而没有走很长的路?

我必须将我的if语句设置为

if(Test == "test1" || Test == "test2" || Test == "test3")
{
    //do something
}
Run Code Online (Sandbox Code Playgroud)

有没有办法有类似的东西

if(Test == "test1":"test2":"test3")
Run Code Online (Sandbox Code Playgroud)

c#

9
推荐指数
3
解决办法
771
查看次数

我可以循环收集Collection.Request.Form吗?

我有一些带有唯一ID的复选框.是否有可能在表单集合中找到所有复选框+ uniquenum?

就像是 -

foreach (var item in Collection.Request.Form["checkbox" + with UniqueIDNum])
{
    //code
}
Run Code Online (Sandbox Code Playgroud)

.net c# asp.net asp.net-mvc

9
推荐指数
1
解决办法
3万
查看次数

.ToString(),(string)或String.什么时候用?

我碰到了一个困扰我的虫子.我有JObject,我认为会很好

obj["role"].ToString()
Run Code Online (Sandbox Code Playgroud)

字符串在那里和一切.最后的手段是改为

(string)obj["role"] 
Run Code Online (Sandbox Code Playgroud)

只是看看会发生什么,它的工作原理.我的问题是我怎么知道何时使用.ToString()而不是(字符串)而不是"as String".

.net c# asp.net asp.net-mvc

9
推荐指数
1
解决办法
3496
查看次数

如何保存已编辑的pdf?

我想保存一个经过编辑的pdf模板.我怎么做?

using (MemoryStream ms = new MemoryStream())  
{    
    PdfReader reader = new PdfReader("~/Content/Documents/Agreement.pdf");
    PdfStamper formFiller = new PdfStamper(reader, ms);
    AcroFields formFields = formFiller.AcroFields;
    formFields.SetField("Name", formData.Name);
    formFields.SetField("Location", formData.Address);
    formFields.SetField("Date", DateTime.Today.ToShortDateString());
    formFields.SetField("Email", formData.Email);
    formFiller.FormFlattening = true;
    formFiller.Close();
}
Run Code Online (Sandbox Code Playgroud)

.net c# pdf asp.net itextsharp

9
推荐指数
1
解决办法
542
查看次数

如何将字典转换为ConcurrentDictionary?

我已经看到如何将ConcurrentDictionary转换为Dictionary,但我有一个字典,并希望转换为ConcurrentDictionary.我该怎么做?...更好的是,我可以将link语句设置为ConcurrentDictionary吗?

var customers = _customerRepo.Query().Select().ToDictionary(x => x.id, x => x);
Run Code Online (Sandbox Code Playgroud)

.net c# asp.net dictionary concurrentdictionary

9
推荐指数
2
解决办法
9907
查看次数

套接字编程有哪些替代方案?

我正在浏览编程故障排除程序,我想知道Socket编程的替代方案是什么; 如果有的话.

c# sockets

8
推荐指数
1
解决办法
6668
查看次数

如何限制MVC中文本框中的字符长度?

我想在MVC中将文本框限制为10个字符.

<label ID="lbl2" runat="server" Width="20px"></label>
<%=Html.TextBox("polNum") %>    
<label ID="lbl1" runat="server" Width="10px"></label>
Run Code Online (Sandbox Code Playgroud)

我知道你可以在.net中设置Max Length属性.如何使用以这种方式生成的文本框在MVC中执行此操作?

.net html c# asp.net asp.net-mvc

8
推荐指数
1
解决办法
3万
查看次数

如何在执行多个查询时保持Connection处于打开状态?

我正在使用多个查询从我的应用程序中的同一服务器中提取数据.问题是我每次有新查询时都必须打开一个新连接.

是否有可能:

  • 打开连接
  • 运行查询
  • 拉结果
  • 运行另一个查询
  • 拉另一个结果
  • 运行最终查询
  • 拉另一个结果
  • 关闭连接.

.net c# asp.net

7
推荐指数
1
解决办法
9104
查看次数

将数字转换为时间?

是否有一种灵活的方式将简单数字(包括陆军时间)转换为时间格式(上午,下午格式)?我可以做一些繁琐的方法,但我只是想知道是否还有另一种方法

0800 =>上午8:00

2317 => 11:17 pm

.net c# asp.net asp.net-mvc

7
推荐指数
1
解决办法
7284
查看次数