gpt4 book ai didi

javascript - 具有多个不同输入的文件上传进度条(MVC)

转载 作者:行者123 更新时间:2023-12-01 01:21:14 24 4
gpt4 key购买 nike

我在互联网上搜索并找到了这个用于文件上传进度条的 JavaScript 和 jQuery 模板,它可以 100% 正常工作(假设您只使用一个表单输入)。

我的情况是,我需要传递一个文件和 4 个其他输入(例如文本)并选择 Controller 操作。 Action 效果很好。我的问题是通过 ajax 将所有这些值传递给 Action,同时保持进度条功能。

操作参数

[HttpPost]
public ActionResult Add_Attachment_to_Process(int id, int Department_id, HttpPostedFileBase Attachment, string sel_checkTask, string cbx_checkTask = null)

HTML

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>

<form method="post" enctype="multipart/form-data" action="/Processes/Add_Attachment_to_Process" id="myform">
<input type="file" id="media" name="file" />


<div class="input-group mb-3">
<div class="input-group-prepend">
<div class="input-group-text">
<input type="checkbox" aria-label="Checkbox for following text input" id="cbx_checkTask" name="cbx_checkTask">
<span id="span_checkTask">Link Task</span>
</div>
</div>
<select class="form-control" id="sel_checkTask" name="sel_checkTask" style="width : 700px;" disabled>
@foreach (var t in Model.User_Tasks)
{
<option value="@t.Task_Discription">@t.Task_Discription - @t.Key_Terms</option>
}
</select>
</div>

<input id="id" name="id" value="@ViewBag.process_id " />
<input id="Department_id" name="Department_id" value="@ViewBag.Department_id" />

<input type="submit" />
</form>

<div class="progress" style="width:40%">
<div id="uploadprogressbar" class="progress-bar" role="progressbar" aria-valuenow="60" aria-valuemin="0" aria-valuemax="100" style="width:0%">
0%
</div>
</div>

JavaScript

$(document).ready(function () {
$("#myform").on('submit', function (event) {
event.preventDefault();

var formData = new FormData($("#myform")[0]);

$.ajax({
xhr: function () {
var xhr = new window.XMLHttpRequest();
xhr.upload.addEventListener('progress', function (e) {

if (e.lengthComputable) {

console.log('Bytes Loaded: ' + e.loaded);
console.log('Total Size: ' + e.total);
console.log('Percentage Uploaded: ' + ((e.loaded / e.total) * 100) + '%');

var percent = Math.round((e.loaded / e.total) * 100);

$("#uploadprogressbar").html(percent + '%');
$("#uploadprogressbar").width(percent + '%');
}

});
return xhr;
},
type: 'POST',
url: '/Processes/Add_Attachment_to_Process',
data: formData,
processData: false,
contentType: false,
success: function () {
alert('File Uploaded');
},
error: function (xhr, status, error) {
var errorMessage = xhr.status + ': ' + xhr.statusText;
alert('Error - ' + errorMessage);
}
});
});
});

最佳答案

根据上面的讨论,尝试这种模式以更好地查看哪些值没有被发送

let f = new FormData();
f.append('id', getYouFormValue("id"));
f.append('sel_checkTask', getYouFormValue("sel_checkTask"));
f.append('cbx_checkTask ', getYouFormValue("cbx_checkTask "));
if (form.File) {
f.append('File', getYouFormValue("file"));
}
const requestOptions = {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: f
}

return fetch(`/Processes/Add_Attachment_to_Process`, requestOptions)
.then(handleResponse)
.then(result => {
//do stuff
});

function handleResponse(response) {
return response.text().then(text => {
const data = text && JSON.parse(text);
if (!response.ok) {
if (response.status === 401) {
console.log('not logged in')
}
const error = (data && data.message) || data.title || response.statusText;
return Promise.reject(error);

}
return data;
});
}

function getYouFormValue(element){
let val = document.getElementById(element);
if(!val){
console.log('empty value');
return null;
}

return val;

}

关于javascript - 具有多个不同输入的文件上传进度条(MVC),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59728506/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com