添加从文件夹photoswipe目的动态读取(Add dynamically reading from a folder photoswipe purpose)
对不起我的英语,我是jquery手机的新手,一般只掌握javascript语言的基本知识; 我正在玩一个单页网站移动(我通常使用Dreamweaver CS6),我用photoswipe达到了一个很好的结果,一切都很好,因为我只有几个图像。 我添加了很多它们,所以现在我会动态地获取图像的链接。
简而言之,我想从我的ftp上的文件夹开始并读取其中的所有图像文件,并为每个文件创建
<li>
项目。 我可以使用jquery mobile完成这项工作,还是应该使用像php或.Net这样的语言我已经在这里和谷歌上阅读了一些例子,但他们并没有帮助我很多,比如这个,我相信它可能是我的答案,但我不知道如何启动http:// docs .phonegap.com / EN / 2.4.0 / cordova_file_file.md.html#DirectoryReader
这里有一些我正在使用的代码:
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.css" /> <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script> <script src="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.js"></script> <!-- Librerie PhotoSwipe --> <link rel="stylesheet" type="text/css" href="../PhotoSwipe/photoswipe.css"> <link rel="stylesheet" type="text/css" href="../PhotoSwipe/styles.css"> <script type="text/javascript" src="../PhotoSwipe/klass.min.js"></script> <script type="text/javascript" src="../PhotoSwipe/code.photoswipe-3.0.5.min.js"></script> <!-- End PhotoSwipe --> <script type="text/javascript"> $(document).ready(function(){ var myPhotoSwipe = $("#Gallery a").photoSwipe({ enableMouseWheel: false , enableKeyboard: false, captionAndToolbarAutoHideDelay: 0 }); }); </script>
然后我的页面
<div data-role="page" id="page"> <div data-role="header"> <h1>Title of my Page</h1> </div> <div data-role="content"> <ul id="Gallery" class="gallery"> <li> <a href="../Images/img04.jpg"> <img src="../Images/img04.jpg" alt=""></a> </li> </ul> </div>
当我登陆这个页面时一切正常。 我要用这样的东西吗? 我从这个网站上获取,我可以使用JSON加入我的ftp文件夹而不是循环内容吗? 我应该把它放在一个功能中吗? 如果是谁将打电话给它?
$("#Photos").live("pagebeforeshow", function(){ $("ul#PhotoList").children().remove('li'); var tag = MyTag $.getJSON("https://api.instagram.com/v1/tags/" + tag + "/media/recent?callback=?&client_id=####", function(data){ $.each(data.data, function(i,item){ $("ul#PhotoList").append('<li><a href="' + item.images.low_resolution.url + ' rel="external"><img src="' + item.images.low_resolution.url + '" alt="' + item.caption.text + '" width="200" /></a></li>'); }); }); var photoSwipeInstance = $("ul#PhotoList a").photoSwipe(); });
任何帮助都是适当的,谢谢你,我相信我的问题是我的有限知识。
Sorry for my English, I am a new to jquery mobile and only have basic knowledge about javascript languages in general; I was playing around with a single page website mobile ( I usually use Dreamweaver CS6) and I reached a good result with photoswipe and everything was good since I had just few images. I have added a lot of them so now I would get the images' link dynamically.
In short, I want to start from a folder on my ftp and read all images file within it and create the
<li>
items for each one. Can I make this job with jquery mobile or should I use a language like php or .NetI have read some examples around here and on google but they didn't help me a lot, like this one, I am sure it could be an answer for me in it but I don't know how to start http://docs.phonegap.com/en/2.4.0/cordova_file_file.md.html#DirectoryReader
Here some code I'm using:
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.css" /> <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script> <script src="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.js"></script> <!-- Librerie PhotoSwipe --> <link rel="stylesheet" type="text/css" href="../PhotoSwipe/photoswipe.css"> <link rel="stylesheet" type="text/css" href="../PhotoSwipe/styles.css"> <script type="text/javascript" src="../PhotoSwipe/klass.min.js"></script> <script type="text/javascript" src="../PhotoSwipe/code.photoswipe-3.0.5.min.js"></script> <!-- End PhotoSwipe --> <script type="text/javascript"> $(document).ready(function(){ var myPhotoSwipe = $("#Gallery a").photoSwipe({ enableMouseWheel: false , enableKeyboard: false, captionAndToolbarAutoHideDelay: 0 }); }); </script>
Then my page
<div data-role="page" id="page"> <div data-role="header"> <h1>Title of my Page</h1> </div> <div data-role="content"> <ul id="Gallery" class="gallery"> <li> <a href="../Images/img04.jpg"> <img src="../Images/img04.jpg" alt=""></a> </li> </ul> </div>
When i land on this page everything works fine. Shall I use something like this? That I took from this website, can I use JSON to accede to my ftp folder and than cycle the content? Should I put this in a function? If yes who is going to call it?
$("#Photos").live("pagebeforeshow", function(){ $("ul#PhotoList").children().remove('li'); var tag = MyTag $.getJSON("https://api.instagram.com/v1/tags/" + tag + "/media/recent?callback=?&client_id=####", function(data){ $.each(data.data, function(i,item){ $("ul#PhotoList").append('<li><a href="' + item.images.low_resolution.url + ' rel="external"><img src="' + item.images.low_resolution.url + '" alt="' + item.caption.text + '" width="200" /></a></li>'); }); }); var photoSwipeInstance = $("ul#PhotoList a").photoSwipe(); });
Any help is appriciated, thank you in advance, I am sure my issue here is my limited knowledge.
原文:https://stackoverflow.com/questions/20594727
最满意答案
问题在于你使用
size()
函数。 它重复调用文件句柄上的fgetc()
,直到它到达文件末尾,递增一个值以跟踪文件中的字节数。这不是一个糟糕的方法(虽然我确信有更好的方法不涉及低效的基于字符的I / O)但它确实有一个你似乎忽略的致命缺陷。
在您调用它之后,您已经将文件一直读到最后,以便进一步读取,例如:
fgets(color, ARR_SIZE, fp);
因为你已经在文件的末尾,所以会失败。 在从
size()
返回之前,您可能需要考虑rewind()
类的东西 - 这会将文件指针放回文件的开头,以便您可以再次读取它。The problem lies in your use of the
size()
function. It repeatedly callsfgetc()
on the file handle until it gets to the end of the file, incrementing a value to track the number of bytes in the file.That's not a bad approach (though I'm sure there are better ones that don't involve inefficient character-based I/O) but it does have one fatal flaw that you seem to have overlooked.
After you've called it, you've read the file all the way to the end so that any further reads, such as:
fgets(color, ARR_SIZE, fp);
will simply fail since you're already at the end of the file. You may want to consider something like
rewind()
before returning fromsize()
- that will put the file pointer back to the start of the file so that you can read it again.
相关问答
更多-
fileopen未初始化。 在您的情况下恰好为空,触发分段错误。 fileopen = open(filename, "r"); if (fileopen) { while (...) { } } fileopen is uninitialized. It so happens to be null in your case, triggering the segmentation fault. fileopen = open(filename, "r"); if (fileopen) { ...
-
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
读取文本文件值(Reading text file values)[2022-02-07]
这个有三个问题: while (fgets(buf,1000, ptr_file)!=NULL) fscanf(buf, "%d,%d\n", &v1, &v2); fgets已经读取数据,因此fscanf不再可用 你使用%d作为格式说明符(整数),但你想读取浮点值(也改变v1和v2的类型!) fscanf的第一个参数是文件流,因此您需要在那里提供ptr_file 。 所以改成它: fscanf(ptr_file, "%f,%f", &v1, &v2); 但请注意,此文本格式可能不适用于 ... -
在PHP中读取文本文件(Reading text file in PHP)[2023-04-08]
$insert = "insert into $name (serial,data,used) values('','fgets($file)','0')"; 您正在将文本文本fgets($file)插入数据库,因为它嵌入在父字符串中。 你想要这样的东西,而且(顺便说一下)它也会删除sql注入漏洞: $string = fgets($file); $string = mysql_real_escape_string($string); $insert = "insert into ... value ... -
使用fgets()和strtok()使用逗号分隔符读取文件(Reading the file with comma delimiter using fgets() and strtok())[2023-05-07]
一旦找到token您永远不会复制token 。 你不能复制linebuffer ,因为当下一行被加载时,那里的数据将被覆盖。 这一行: number[ctr] = linebuffer; 应该引用token来保存最近找到的令牌,但事实并非如此。 它可能应该读为1 : strcpy(number[ctr], token); 但是你必须改变声明以确保空间: char number[3][32]; 显然,这会引入缓冲区溢出风险,如果有一个很长的令牌,它就不适合。 如何最好地处理这是一个练习。 :) 1为什么 ... -
问题在于你使用size()函数。 它重复调用文件句柄上的fgetc() ,直到它到达文件末尾,递增一个值以跟踪文件中的字节数。 这不是一个糟糕的方法(虽然我确信有更好的方法不涉及低效的基于字符的I / O)但它确实有一个你似乎忽略的致命缺陷。 在您调用它之后,您已经将文件一直读到最后,以便进一步读取,例如: fgets(color, ARR_SIZE, fp); 因为你已经在文件的末尾,所以会失败。 在从size()返回之前,您可能需要考虑rewind()类的东西 - 这会将文件指针放回文件的开头,以便您 ...
-
那么,你可以检查两个显而易见的事情: 文件的大小( FileInfo.Length ) 最后写入时间( FileSystemInfo.LastWriteTimeUtc ) 如果你跟踪这些,你应该能够检测文件何时发生变化 - 至少在合理的置信度下。 此外,您可以使用FileSystemWatcher来监视更改。 此外,您可能需要考虑跟踪您所读取的位置 - 因此您可以通过寻找文件中的正确位置来阅读新数据。 最后, TextBox可能不是真正的巨大日志文件的最佳用户界面。 如果这是一个结构化的日志文件,那么在UI ...
-
来自IEEE Std1800-2012§21.3.4.2一次读一行 : 可以使用$fgets从文件中读取一行。 例如: integer code; code = $fgets ( str, fd ); 将 fd指定的文件中的字符读入变量 str直到 str被填充,或者读取换行符并将其传送到 str ,或者遇到 EOF条件。 如果 str长度不是整数个字节,则不使用最重要的部分字节来确定大小。 如果从文件读取错误,则 code设置为零。 否则,读取的字符数将以 code形式返回。 应用程序可以调用 $ferr ...
-
这里有两个问题: 混合fscanf()和fgets()是一个坏主意,因为前者在一条线上工作,后者在一条完整线上工作。 float并不像你期望的那样精确。 解决1: fscanf(donnees, "%d", &num[n]); fscanf(donnees, "%s", nom[n]); fscanf(donnees, "%s", pre[n]); fscanf(donnees, "%f\n", ¬e[n]); 为了避免溢出“字符串”,您可以告诉fscanf()以最大值扫描多少,例如,对于"%42s ...
-
Scanner类有一个构造函数Scanner(InputStream) ; 所以你仍然可以像以前一样使用这个类来读取数据。 你所要做的就是从Jar中读取文件,你可以这样做: InputStream is = getClass().getResourceAsStream("champdata.txt"); Scanner read = new Scanner(is); read.useDelimiter("%"); 如果名为champdata.txt的文件位于jar文件的根目录(这只是一个zip文件,则可以 ...