Khe*_*pri 28 .net asp.net-mvc httppostedfilebase asp.net-mvc-3
问题的背景.
编辑3 我可以验证这似乎在Chrome 20中再次运行.谢谢!
TLDR更新
编辑2 进一步捣乱后,这似乎是Chrome 19中的一个错误.请看这里:
没有什么比最新的bug了!:-)
Firefox-latest和IE8/9按预期工作.Fiddler日志显示相同的行为,主要区别在于第二次401授权检查不会发送表格有效负载乱码并且它按预期工作.
这听起来像一个测试用例,修复工作正在进行中!所以,希望对于那些可能遇到过这种情况的人来说,这会有所帮助!
结束TLDR
我有一个控制器操作,只接受一个上传的文件,这是一个"事件"的CSV文件.在幕后,此CSV被解析出来,变成了事件对象,数据在数据库中同步.结果输出时,系统会提示用户下载Excel电子表格,其中包含发生的每一行的所有错误.
这在我的开发机器上本地工作正常.然而,一旦部署到DEV环境,我会得到不同的结果.上传文件的第一次尝试导致流是什么,我在这里寻找的单词是什么,不可读?它正确地报告了它的长度,但试图将信息拉出来却一无所获.后续读取确实有数据,一切都按预期工作.
我会告诉你相关的代码pices.首先,简化了gen的HTML表单:
<form action="/Events/ImportLocal" enctype="multipart/form-data" method="post">
<input id="uploadFile" name="uploadFile" type="file" />
<input type="submit" value="Upload Events" />
</form>
Run Code Online (Sandbox Code Playgroud)
非常直截了当:
控制器动作(原谅这个烂摊子,我现在已经乱砍了几个小时了):
[HttpPost]
public ActionResult ImportLocal(HttpPostedFileBase uploadFile)
{
if (uploadFile == null || uploadFile.ContentLength <= 0)
{
BaseLogger.Info("uploadFile is null or content length is zero...");
//Error content returned to user
}
BaseLogger.InfoFormat("Community Import File Information: Content Length: {0}, Content Type: {1}, File Name: {2}",
uploadFile.ContentLength, uploadFile.ContentType, uploadFile.FileName);
//This logger line is always reporting as correct. Even on the attempts where I can't pull out the stream data, I'm seeing valid values here.
//EXAMPLE output on failed attempts:
//Import File Information: Content Length: 315293, Content Type: application/vnd.ms-excel, File Name: Export_4-30-2012.csv
BaseLogger.InfoFormat("Upload File Input Stream Length: {0}", uploadFile.InputStream.Length);
//This is reporting the correct length on failed attempts as well
//I know the below is overly complicated and convoluted but I'm at a loss for why the inputstream in HttpPostedFileBase is not pulling out as expected
//so I've been testing
var target = new MemoryStream();
uploadFile.InputStream.CopyTo(target);
byte[] data = target.ToArray();
BaseLogger.InfoFormat("File stream resulting length = {0}", data.Length);
//This reports correctly. So far so good.
StringReader stringOut;
var stream = new MemoryStream(data) {Position = 0};
using (var reader = new StreamReader(stream))
{
string output = reader.ReadToEnd();
BaseLogger.InfoFormat("Byte[] converted to string = {0}", output);
//No go...output is reported to be empty at this point so no data ever gets sent on to the service call below
stringOut = new StringReader(output);
}
//Build up a collection of CommunityEvent objects from the CSV file
ImportActionResult<Event> importActionResults = _eventImportServices.Import(stringOut);
Run Code Online (Sandbox Code Playgroud)
目标是将文本阅读器或字符串阅读器传递给服务方法,因为幕后这是使用需要这些类型的CSV处理实现.
任何想法为什么第一个请求失败但后续工作?我觉得我需要一双新鲜的眼睛,因为我的想法一直在干涸.
最后一点,IIS 7.5是本地通过IIS Express和目标服务器上的目标.
谢谢
编辑赏金:
我在这里复制我的赏金信息和每个请求的Fiddler输出
我在这个问题上添加了一些评论.该问题似乎与NTLM有关.我在Fiddler看到的是401 Unauthorized on request 1 with valid form data(点,我的理解是这些前两个401 Unauthorized请求在只有Windows身份验证的情况下是"正常的";请验证吗?).列出的请求2再次是具有相同表单数据的401,除了上传的文件数据现在只是一堆框,完全相同大小的数据,而不是确切的上传数据.请求3是200 OK,包含乱码数据,这就是我的控制器操作.如何让NTLM在文件上传方面发挥出色?
这是每个请求的Fiddler输出:
要求1)

要求2)

最后请求3即已处理的HTTP 200 OK

编辑2 进一步捣乱后,这似乎是Chrome 19中的一个错误.请看这里:
没有什么比最新的bug了!:-)
Firefox-latest和IE8/9按预期工作.Fiddler日志显示相同的行为,主要区别在于第二次401授权检查不会发送表格有效负载乱码并且它按预期工作.
这听起来像一个测试用例,修复工作正在进行中!所以,希望对于那些可能遇到过这种情况的人来说,这会有所帮助!
谢谢
这只是一个尝试的想法。请注意,StreamReader 有许多构造函数:
http://msdn.microsoft.com/en-us/library/system.io.streamreader.aspx
在某些构造函数中,它尝试自动检测编码(如果指定),如果无法检测到编码,则它会回退到指定的编码。使用的构造函数使用UTF-8编码。您可以更改构造函数以自动检测编码,如果没有,则使用 UTF-8。
| 归档时间: |
|
| 查看次数: |
3086 次 |
| 最近记录: |