Im having trouble with uploading multiple. i want to store the path of file into db using ef6 if i upload a single file it works just fiine but when i try to upload multiple files using jquery file upload: the files are sent but are not saved into database (the are added to context) but savechanges is called for first file only. it some times throws

我上传多个时遇到问题。我想使用ef6将文件的路径存储到数据库中,如果我上传单个文件它只能工作但是当我尝试使用jquery文件上传上传多个文件时:文件被发送但不保存到数据库中(被添加到数据库中) context)但是仅为第一个文件调用savechanges。它有时会抛出

(sql connection dose not support multiple transactions)

and some times it dosnt. ?!? (strange) here is my upload action:

有时它是dosnt。 ?!? (奇怪)这是我的上传动作:

    [HttpPost]
    public ActionResult Upload(HttpPostedFileBase file)
    {
        try
        {
            Video video = _repository.Upload<Video>(Request.Files[0]);
            video.CategoryId = int.Parse(Request.Form["CategoryId"]);
            int result = _unitOfWork.Commit();
            return Json(new
            {
                Success = result > 0,
                Message = "Successfully uploaded..."
            });
        }
        catch (Exception ex)
        {
            return Json(new
            {
                Success = false,
                Message = ex.Message
            });
        }
    }

and here is my upload method:

这是我的上传方法:

public static T Upload<T>(this IRepository<T> repository, HttpPostedFileBase file)
        where T : DomainEntities.File, IEntity, new()
    {
        const string photoPath = "~/Uploads/Photos/";
        const string audioPath = "~/Uploads/Audios/";
        const string videoPath = "~/Uploads/Videos/";

        Type type = typeof(T);

        T t = new T()
        {
            Date = DateTime.Now,
            Name = file.FileName,
            Size = file.ContentLength.ToString(),
            ContentType = file.ContentType
        };

        if (type == typeof(Photo))
        {
            t.Path = Path.Combine(photoPath, file.FileName);
        }
        else if (type == typeof(Video))
        {
            t.Path = Path.Combine(videoPath, file.FileName);
        }
        else if (type == typeof(Audio))
        {
            t.Path = Path.Combine(audioPath, file.FileName);
        }

        file.SaveAs(HttpContext.Current.Server.MapPath(t.Path));
        repository.Add(t);
         //some times its written correctly equal to number of files but some times not.
        Debug.WriteLine("Added"); 
        return t;
    }

Update: the files are correctly saveing into specified file system path btw.

更新:文件正确保存到指定的文件系统路径顺便说一句。

and this is the view:

这是观点:

<div class="portlet-body form">
    <form id="fileupload" class="form-horizontal" action="/Files/Video/Upload" method="POST" enctype="multipart/form-data">
        <!-- The fileupload-buttonbar contains buttons to add/delete files and start/cancel the upload -->
        <div class="row fileupload-buttonbar">

            <div class="col-lg-7">
                <!-- The fileinput-button span is used to style the file input field as button -->
                <span class="btn green fileinput-button">
                    <i class="fa fa-plus"></i>
                    <span>
                         select
                    </span>
                    <input type="file" name="files[]" multiple>
                </span>
                <button type="submit" class="btn blue start">
                    <i class="fa fa-upload"></i>
                    <span>
                        Upload
                    </span>
                </button>
                <button type="reset" class="btn yellow cancel">
                    <i class="fa fa-ban"></i>
                    <span>
                        cancel
                    </span>
                </button>
                <input type="checkbox" class="toggle">
                <!-- The loading indicator is shown during file processing -->
                <span class="fileupload-loading">
                </span>
            </div>
            <!-- The global progress information -->
            <div class="col-lg-5 fileupload-progress fade">
                <!-- The global progress bar -->
                <div class="progress progress-striped active" role="progressbar" aria-valuemin="0" aria-valuemax="100">
                    <div class="progress-bar progress-bar-success" style="width:0%;">
                    </div>
                </div>
                <!-- The extended global progress information -->
                <div class="progress-extended">
                    &nbsp;
                </div>
            </div>
        </div>
        <!-- The table listing the files available for upload/download -->
        <table role="presentation" class="table table-striped clearfix">
            <tbody class="files"></tbody>
        </table>
    </form>

</div>

thanks for help.

感谢帮助。

1 个解决方案

#1


0

I think, you have 2 options, I'm confident that the second one is the best:

我想,你有2个选择,我相信第二个是最好的选择:

Try to work with the request object

尝试使用请求对象

foreach (string file in Request.Files)
{
    //use the Request.Files[file]
}

Or you can redefine the input name int his way

或者您可以按照自己的方式重新定义输入名称

<input type="file" name="files" value="" multiple="multiple"/>

without the array definition, and you can try refactoring your action in this way

没有数组定义,您可以尝试以这种方式重构您的操作

public ActionResult Upload(HttpPostedFileBase[] files)
    foreach (var file in files) {
        if (file.ContentLength > 0) {
            // Save your file
        }
    }
}

It should work.

它应该工作。

更多相关文章

  1. 为什么我不能在同一个js文件中建立两个cleave.js格式的字段?
  2. (文件).ready是全球范围的吗?
  3. 当AJAX响应来自PHP文件时,如何显示以消息为中心的加载器图像&防止
  4. JQuery------获取中的文件内容
  5. 如何在Rails 4(使用jquery)中上传多个文件?
  6. jQuery 异步上传插件 Uploadify302 使用 (JavaEE Spring MVC)
  7. 使用jquery post发布PHP文件
  8. Jquery+Aajax 批量上传
  9. 从JQuery文件输入中获取数据

随机推荐

  1. android studio系列之使用指南
  2. Android 支付宝App支付集成
  3. Android 分页组件
  4. android反编译与加固(Mac版)
  5. Android常见布局简述
  6. Android中文文档v0.1 beta低调发布,期待
  7. Android可收缩/扩展的TextView【2】
  8. 《宅男的android开发指南》(翻译)--1
  9. android ViewPager不可滑动(android 项目
  10. Android(安卓)适配不同分辨率&hdpi、mdpi