gpt4 book ai didi

c# - OpenXml 无法打开包,因为 FileMode 或 FileAccess 值对流无效

转载 作者:太空狗 更新时间:2023-10-29 18:29:17 39 4
gpt4 key购买 nike

流来自 html 表单,通过 ajax var jqXHR = data.submit();

public static GetWordPlainText(Stream readStream,string filePath)
{
WordprocessingDocument.Open(readStream, readStream.CanRead);
}
[HttpPost]
public ActionResult FileUpload()
{
var MyFile = Request.Files[0];
if (Request.Files.Count > 0 && MyFile != null)
{
GetWordPlainText(Request.InputStream);
}
}

我收到这个错误:

Cannot open package because FileMode or FileAccess value is not valid for the stream.

我用谷歌搜索 无法打开包,因为 FileMode 或 FileAccess 值对流无效 但找不到任何有用的东西。有什么想法吗?

PS:最初我简化了这里发布的代码。添加了 if 语句,以便消除 Sten Petrov 的担忧。我希望 Request.File.count>0 确实解决了他的问题……我仍然有同样的问题……

更新

作为解决方法,我按照下面的建议将文件保存到一个目录,然后我使用 openxml 从目录中读取它

  var MyFile = Request.Files[0];
var path = Path.Combine(Server.MapPath("~/App_Data/temp"), MyFile.FileName);
using (MemoryStream ms = new MemoryStream())
{
//if file exist plz!!!! TODO

Request.Files[0].InputStream.CopyTo(ms);
System.IO.File.WriteAllBytes(path, ms.ToArray());
}

然后 WordprocessingDocument.Open 有一个文件路径的实现所以WordprocessingDocument.Open(path); 希望您了解我为 future 遇到问题的人所做的工作。

最佳答案

你这样做是自找麻烦,因为 Request 流可能没有完全下载。

我建议您先将文件下载到 MemoryStream 中或作为文件下载,see here对于后一个选项,然后对上传的文件执行任何操作。

关于c# - OpenXml 无法打开包,因为 FileMode 或 FileAccess 值对流无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17888076/

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com