为什么IEnumerable的< HttpPostedFileBase>计数为1当我上传0文件? [英] Why IEnumerable<HttpPostedFileBase> count is 1 when I upload 0 files?

查看:642
本文介绍了为什么IEnumerable的< HttpPostedFileBase>计数为1当我上传0文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个上传的形式,我想检查是否有任何文件,当我启动上传。这里是我的code。

查看:

  @using(Html.BeginForm(上传,家,FormMethod.Post,
                       新{ENCTYPE =的multipart / form-data的}))
{
    <输入名字=文件类型=文件多个=多​​/>
    <输入类型=提交值=上传/>
}

控制器:

  [HttpPost]
公众的ActionResult上传(IEnumerable的< HttpPostedFileBase>文件)
{
    如果(files.Count()&0)Console.WriteLine(files.Count()); //显示1
    如果(files.Any())Console.WriteLine(files.Any()); //显示真
    如果(files.First()== NULL)Console.WriteLine(第一空); //显示第一个空    返回查看();
}

为什么我的程序显示效果一样,当我提出一个空的形式?
我可能会用JS我场检查,但我想了解什么是我的的IEnumerable&LT这些数据; HttpPostedFileBase> 。谢谢你。


解决方案

虽然我有点晚了党,但仍。
我有一个同样的问题。发现asp.net的文章,他们说,它的设计。
HTTP://aspnetwebstack.$c$cplex.com/workitem/188


  

这是设计使然,因为请求包含有文件名=那段。如果你不希望有创建的文件,请删除该请求段。
  我通过以下方式固定它。


 如果(RelatedFiles.Any())
            {
                的foreach(在RelatedFiles var文件)
                {
                    如果(文件!= NULL)//这里只是检查空值。
                    {
                        字节[] = UploadedFile的新的字节[file.InputStream.Length]
                        file.InputStream.Read(UploadedFile的,0,file.ContentLength);
                        FileInfo的网络连接=新的FileInfo(file.FileName);                        VAR上传=新UploadedFile的
                        {
                            的ContentType = file.ContentType,
                            内容= UploadedFile的,
                            文件名= fi.Name,
                            ContentExtension = fi.Extension,
                        };                        newIssuePaper.RelatedDocuments.Add(上传);
                    }
                }

I have a multiple upload form and I want to check if there is any files when I launch the upload. Here is my code.

View :

@using (Html.BeginForm("Upload", "Home", FormMethod.Post, 
                       new { enctype = "multipart/form-data"}))
{
    <input name="files" type="file" multiple="multiple" />
    <input type="submit" value="Upload" />
}

Controller :

[HttpPost]
public ActionResult Upload(IEnumerable<HttpPostedFileBase> files)
{
    if (files.Count() > 0) Console.WriteLine(files.Count()); // display 1
    if(files.Any()) Console.WriteLine(files.Any()); // display true
    if (files.First() == null) Console.WriteLine("first null"); // display "first null"

    return View();
}

Why my program display results like that when I submit an empty form ? I'll probably check with JS my field, but I want to understand what is these data in my IEnumerable<HttpPostedFileBase>. Thank you.

解决方案

Though i am a little late for the party but still. I had a same issue. Found an article on asp.net they said that its by design. http://aspnetwebstack.codeplex.com/workitem/188

This is by design because the request contains that segment which has filename="". If you don't want to have the file created, please remove that segment from the request. I fixed it via the following way.

 if (RelatedFiles.Any())
            {
                foreach (var file in RelatedFiles)
                {
                    if (file != null) // here is just check for a null value.
                    {


                        byte[] uploadedFile = new byte[file.InputStream.Length];
                        file.InputStream.Read(uploadedFile, 0, file.ContentLength);
                        FileInfo fi = new FileInfo(file.FileName);

                        var upload = new UploadedFile
                        {
                            ContentType = file.ContentType,
                            Content = uploadedFile,
                            FileName = fi.Name,
                            ContentExtension = fi.Extension,
                        };

                        newIssuePaper.RelatedDocuments.Add(upload);
                    }
                }

这篇关于为什么IEnumerable的&LT; HttpPostedFileBase&GT;计数为1当我上传0文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆