Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/422.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在JQuery AJAX文件上传中添加唯一标识符?_Javascript_C#_Jquery_Asp.net_Asp.net Mvc - Fatal编程技术网

Javascript 如何在JQuery AJAX文件上传中添加唯一标识符?

Javascript 如何在JQuery AJAX文件上传中添加唯一标识符?,javascript,c#,jquery,asp.net,asp.net-mvc,Javascript,C#,Jquery,Asp.net,Asp.net Mvc,我使用的代码在asp.net mvc应用程序中运行良好 在标题部分: <script type="text/javascript"> $(document).ready(function () { $('#fileupload').fileupload({ dataType: 'json', url: '@Url.Action("UploadFile", "Payment")', auto

我使用的代码在asp.net mvc应用程序中运行良好

在标题部分:

<script type="text/javascript">
    $(document).ready(function () {
        $('#fileupload').fileupload({
            dataType: 'json',
            url: '@Url.Action("UploadFile", "Payment")',
            autoUpload: true,
            done: function (e, data) {
                $('.file_name').html(data.result.name);
                $('.file_type').html(data.result.type);
                $('.file_size').html(data.result.size);
            }
        }).on('fileuploadprogressall', function (e, data) {
            var progress = parseInt(data.loaded / data.total * 100, 10);
            $('.progress .progress-bar').css('width', progress + '%');
        });
    });
</script>

$(文档).ready(函数(){
$('#fileupload')。fileupload({
数据类型:“json”,
url:'@url.Action(“上传文件”,“付款”),
自动上传:对,
完成:功能(e,数据){
$('.file_name').html(data.result.name);
$('.file_type').html(data.result.type);
$('.file_size').html(data.result.size);
}
}).on('fileuploadprogressall',函数(e,数据){
var progress=parseInt(data.loaded/data.total*100,10);
$('.progress.progress bar').css('width',progress+'%');
});
});
身体使用部分:

    <span>
        <span class="btn btn-success fileinput-button" style="width: 200px; float: left;">
            <span>Certificate: </span>
            <input id="fileupload" type="file" name="files[]" multiple>
        </span>
    </span>

证书:
问题是,正文中会有许多使用站点,每个站点用于具有不同标识的对象,因此当服务器上的操作响应文件上载时,它还需要为对象的id获取一个额外参数:

    <span>
        <span class="btn btn-success fileinput-button" style="width: 200px; float: left;">
            <span>Certificate for Object ID=100: </span>
            <input id="fileupload" type="file" name="files[]" multiple>
        </span>
    </span>  
    <span>
        <span class="btn btn-success fileinput-button" style="width: 200px; float: left;">
            <span>Certificate for Object ID=101: </span>
            <input id="fileupload" type="file" name="files[]" multiple>
        </span>
    </span>  
    <span>
        <span class="btn btn-success fileinput-button" style="width: 200px; float: left;">
            <span>Certificate for Object ID=102: </span>
            <input id="fileupload" type="file" name="files[]" multiple>
        </span>
    </span>  

对象ID=100的证书:
对象ID=101的证书:
对象ID=102的证书:
如何添加要发布到服务器的附加对象Id参数

以下是对付款控制器代码的操作:

    [HttpPost]
    public ContentResult UploadFile()
    {
        var r = new List<UploadFilesResult>();

        foreach (string file in Request.Files)
        {
            HttpPostedFileBase hpf = Request.Files[file] as HttpPostedFileBase;
            if (hpf.ContentLength == 0)
                continue;

            string savedFileName = Path.Combine(Server.MapPath("~/App_Data"), Path.GetFileName(hpf.FileName));
            hpf.SaveAs(savedFileName);

            r.Add(new UploadFilesResult()
            {
                Name = hpf.FileName,
                Length = hpf.ContentLength,
                Type = hpf.ContentType
            });
        }
        return Content("{\"name\":\"" + r[0].Name + "\",\"type\":\"" + r[0].Type + "\",\"size\":\"" + string.Format("{0} bytes", r[0].Length) + "\"}", "application/json");
    }
[HttpPost]
公共ContentResult上载文件()
{
var r=新列表();
foreach(Request.Files中的字符串文件)
{
HttpPostedFileBase hpf=Request.Files[file]作为HttpPostedFileBase;
如果(hpf.ContentLength==0)
继续;
字符串savedFileName=Path.Combine(Server.MapPath(“~/App_Data”)、Path.GetFileName(hpf.FileName));
hpf.SaveAs(savedFileName);
r、 添加(新上载文件结果()
{
Name=hpf.FileName,
长度=hpf.ContentLength,
Type=hpf.ContentType
});
}
返回内容(“{\”名称\“:\”+r[0]。名称+“\”,\”类型\“:\”+r[0]。类型+“\”,\”大小\“:\”+字符串。格式(“{0}字节”,r[0]。长度)+“\”,“应用程序/json”);
}

尝试将类作为标识符,而不是使用id

使用类,并在jquery选择器中:
$('.fileupload')
带有
id=“fileupload”
的3个元素是无效的html(并且只有第一个元素可以工作)。你能给他们起个不同的名字吗
name=“files-100[]”
name=“files-101[]”
这不是一个变量-它只是意味着你可以通过
Request.files[files-100]
等方式访问它们,但我不确定这是否能满足你的需要-我有点不确定你想做什么do@Arjang,查看,您应该能够使用
$('#fileupload1').fileupload({formData:{id:100},url:'@url.Action(“UploadFile”,“Payment”),…})
并将
int-id
作为参数包含在
public ContentResult-UploadFile(int-id){..}
中-您需要给每个元素一个唯一的
id
,这样您就可以设置
formData:{id:}
您还应该将方法更改为
public JsonResult UploadFile(int-id)
并使用
返回Json(新的{name=r[0].name,type=r[0].type等)
并使用
数据访问它。name
等。谢谢,Ehsan Sajjad也建议,但我如何传递一个额外的参数?