ajax jquery - 将 csv 文件一次发送到 php 一行
我想这样做:
- 用户加载一个csv文件
- ,它一一读取数据,
- php进行处理,需要相当长的时间
- php进入mysql的
过程问题是:
- csv文件太大,所以php打了30秒时限。
- 处理后的数据应该按顺序输入到sql中,并一一
- 运行php,与mysql的连接无法跟上,到目前为止,我需要将csv分成每个文件30行,以便可以正确输入到mysql 。
我认为与 jquery 结合:
- jquery 读取文件
- jquery 每 csv 行暂停 1 秒
- jquery 是客户端,所以它没有时间限制 jquery
- 然后调用 php,后者将发送到 mysql
我被 jquery 困住了,因为它发送csv 中的数据以非排序方式。
我需要帮助的是在示例中将 jquery.post 逐一暂停,每次间隔 1 秒。
这是我到目前为止提出的代码。我需要每个帖子的“暂停”效果
<script>
$(function () {
var o1;
var o2;
$.ajaxSetup({cache: false});
$('#load').click(function () {
$.get($('#file').val(), function (content) {
//get the file and convert into single array
o1 = content.split(new RegExp("\n")).map(function (element) {return $.trim(element).toLowerCase();})
//---------------------------------------------------------------------------- Display
$("#box").empty();
$.each(o1,
function( i, j ){
if (j!=""){
//add the tr
//$("#tbl").append($( "<tr id=\"tr-"+i+"\"></tr>" ));
//split again
o2 = j.split(new RegExp(",|\r")).map(function (element) {return $.trim(element).toLowerCase();});
$.post('test2.php',{jquery:'1' , d:o2[0] , o:o2[1] , h:o2[2] , l:o2[3] , c:o2[4]},function(res){
$("#box").append(res);
});
}//end if
}//end function
);//end each
//---------------------------------------------------------------------------- end of display
});
});
});
</script>
</head>
<body>
<form>
<input id="file" type="file" />
<input type="button" id="load" value="Load CSV" />
</form>
<hr />
<table id="tbl" border="1"><tbody></tbody></table>
<div id="box"></div>
<ol id="list"></ol>
</body>
,这是“test2.php”文件内容。这只是我要做的一个例子,真正的代码当然更复杂:
if ($_POST){
@$_POST['jquery']!=''?$jquery=$_POST['jquery']:$jquery='';
if ($jquery!=''){
@$_POST['d']!=''?$d=$_POST['d']:$d='';
@$_POST['o']!=''?$o=$_POST['o']:$o='';
@$_POST['h']!=''?$h=$_POST['h']:$h='';
@$_POST['l']!=''?$l=$_POST['l']:$l='';
@$_POST['c']!=''?$c=$_POST['c']:$c='';
echo 'D: '.$d.' - '.$o.' - '.$h.' - '.$l.' - '.$c."<br />\n";
}
}
这里真的需要帮助。谢谢。
I want to do this:
- user load a csv file
- it reads the data one by one
- php do the processing, it takes quite some time
- php enter the process to mysql
problems are:
- csv file is too large, so the php hit the 30 secs time limit.
- the processed data should be entered to sql in order and one by one
- running just the php, the connection to mysql can't keep up, so far i need to separate the csv to 30 lines per file so it could be entered correctly to mysql.
I think combining with jquery:
- jquery reads the file
- jquery pause 1 second per csv line
- jquery is client side so it won't have time limitation
- jquery then call php which in turn will send to mysql
I'm stuck with jquery, because it sends the data in csv in non-ordered way.
What I need help is to pause the jquery.post one by one in example 1 second interval each.
here's the code that i've come up so far. i need the "pausing" effect for each post
<script>
$(function () {
var o1;
var o2;
$.ajaxSetup({cache: false});
$('#load').click(function () {
$.get($('#file').val(), function (content) {
//get the file and convert into single array
o1 = content.split(new RegExp("\n")).map(function (element) {return $.trim(element).toLowerCase();})
//---------------------------------------------------------------------------- Display
$("#box").empty();
$.each(o1,
function( i, j ){
if (j!=""){
//add the tr
//$("#tbl").append($( "<tr id=\"tr-"+i+"\"></tr>" ));
//split again
o2 = j.split(new RegExp(",|\r")).map(function (element) {return $.trim(element).toLowerCase();});
$.post('test2.php',{jquery:'1' , d:o2[0] , o:o2[1] , h:o2[2] , l:o2[3] , c:o2[4]},function(res){
$("#box").append(res);
});
}//end if
}//end function
);//end each
//---------------------------------------------------------------------------- end of display
});
});
});
</script>
</head>
<body>
<form>
<input id="file" type="file" />
<input type="button" id="load" value="Load CSV" />
</form>
<hr />
<table id="tbl" border="1"><tbody></tbody></table>
<div id="box"></div>
<ol id="list"></ol>
</body>
and this is the 'test2.php' file content. it's just an example of what i will do, the real code is of course more complex:
if ($_POST){
@$_POST['jquery']!=''?$jquery=$_POST['jquery']:$jquery='';
if ($jquery!=''){
@$_POST['d']!=''?$d=$_POST['d']:$d='';
@$_POST['o']!=''?$o=$_POST['o']:$o='';
@$_POST['h']!=''?$h=$_POST['h']:$h='';
@$_POST['l']!=''?$l=$_POST['l']:$l='';
@$_POST['c']!=''?$c=$_POST['c']:$c='';
echo 'D: '.$d.' - '.$o.' - '.$h.' - '.$l.' - '.$c."<br />\n";
}
}
Really need help here. thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尽管我不是您处理问题的方式的坚定支持者...
阻止 jQuery 进行异步调用:
async: false
您可以这样做在 jQuery 中:
$.ajaxSetup({async:false});
,然后对$.post
发出常规调用。 有关手册的更多信息。Even though I'm not a strong supporter of the way you're approaching the problem...
to STOP jQuery from making assync calls:
async: false
You can do this in jQuery:
$.ajaxSetup({async:false});
and then issue your regular call to$.post
. More info on the manual.