mvc3 c#streamreader文件无法读取(mvc3 c# streamreader file not reading)

编程入门 行业动态 更新时间:2024-10-16 22:14:03
mvc3 c#streamreader文件无法读取(mvc3 c# streamreader file not reading)

我有一个文件,用户浏览并点击上传按钮,我将文件保存在服务器上,appdata / uploads在应用程序目录中。 然后我尝试使用流阅读器阅读它,然后解析它。 在我的本地开发环境中它工作正常,但是当我在服务器上部署它时根本不起作用。 有什么建议么?? 谢谢

//Save LoadList File: DateTime uploadDate = DateTime.Now; string destinationPath = string.Format("{0}\\{1}\\{2}\\{3}\\", Server.MapPath("~/App_Data/uploads"), uploadDate.ToString("yyyy"), uploadDate.ToString("MMM"), uploadDate.ToString("dd")); if (!Directory.Exists(destinationPath)) Directory.CreateDirectory(destinationPath); string storedFileName = string.Format("{0}{1}.json", destinationPath, System.Guid.NewGuid()); file.ElementAt(0).SaveAs(storedFileName); //FileImport is a static class var Pair = FileImport.CyclesCompleted(storedFileName); private static string LoadTextFromFile(string fileName) { StreamReader streamReader = new StreamReader(fileName); string text = streamReader.ReadToEnd(); streamReader.Close(); return text; }

I have a file, which the users browse and hit the upload button, i save the file on the server, appdata/uploads in the application directory. then i try to read it using stream reader and then parse it. on my local development enviornment it works fine, but when i deploy it on a server it does not work at all. any suggestions?? thank you

//Save LoadList File: DateTime uploadDate = DateTime.Now; string destinationPath = string.Format("{0}\\{1}\\{2}\\{3}\\", Server.MapPath("~/App_Data/uploads"), uploadDate.ToString("yyyy"), uploadDate.ToString("MMM"), uploadDate.ToString("dd")); if (!Directory.Exists(destinationPath)) Directory.CreateDirectory(destinationPath); string storedFileName = string.Format("{0}{1}.json", destinationPath, System.Guid.NewGuid()); file.ElementAt(0).SaveAs(storedFileName); //FileImport is a static class var Pair = FileImport.CyclesCompleted(storedFileName); private static string LoadTextFromFile(string fileName) { StreamReader streamReader = new StreamReader(fileName); string text = streamReader.ReadToEnd(); streamReader.Close(); return text; }

最满意答案

由于大多数帐户无法写入服务器上的默认位置,因此通常在服务器上保存文件会导致权限错误。 您可以使用Path.GetTempFileName ,但即使在这种情况下,某些帐户(即请求为“匿名用户”运行的帐户)也无权读取/写入该位置。

如果您只需要解析上传的文件,则可以将流复制到MemoryStream并在此内存流上创建StreamReader。 您可以直接使用Stream上传文件,但它不支持seaking(可能因为您使用不寻求的StreamReader而工作)。

Saving file on server ususlly results in permission errors since most accounts can't write to default location on server. You may get away with using Path.GetTempFileName, but even in this case some accounts (i.e. account that requests run for "anonymous user") will not have permissions to read/write to that location.

If you simply need to parse uploaded file you can copy stream to MemoryStream and create StreamReader over this memory stream. You may be able to use Stream for uploaded file directly, but it will not support seaking (may work as you are using StreamReader which does not seek).

更多推荐

本文发布于:2023-07-16 12:52:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1128648.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   streamreader   file   reading

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!