简体   繁体   English

我如何允许用户将文件上传到我的网站,对其进行编辑并将其保存回去

[英]How do I allow the user to upload file to my site, edit it and save it back

I have a very simple web app I am building. 我正在构建一个非常简单的Web应用程序。 no php - just html & javascript. 没有php-只有html和javascript。

I want to get this use case for user who comes to my site: 我想为来我网站的用户获得此用例:

  1. the user will choose a text file from his/her computer - for example: c:\\directory\\file.txt 用户将从他/她的计算机中选择一个文本文件-例如:c:\\ directory \\ file.txt
  2. he will open this file in my webpage (do I have to use php for this? to upload the file to the server and then display it on my website? If I don't want the data in my server - just to let the user edit it - can I just do all these actions with js on client-side?) 他将在我的网页中打开此文件(我是否必须为此使用php?将文件上传到服务器,然后在我的网站上显示?如果我不希望服务器中的数据-只是为了让用户编辑它-我可以在客户端使用JS执行所有这些操作吗?)
  3. he will edit some of the text 他将编辑一些文本
  4. he will press a button called "save" or "save as" and it will automatically save his file in his computer. 他将按下名为“保存”或“另存为”的按钮,它将自动将文件保存在计算机中。

since I am clueless in file management - I'd be happy to get a tip where to start advice on js libraries that manage that or general information about that. 由于我对文件管理一无所知-我很高兴获得提示,以获取有关管理该库或有关该库的一般信息的js库的建议。

thank, Alon 谢谢,阿隆

ps searching the web for "upload files" gave me a lot of result on how to upload files to ftp - my question is not about that. ps在网络上搜索“上传文件”给了我很多关于如何将文件上传到ftp的结果-我的问题不是这个。

What you seem to be asking for typically requires server side code like php. 您似乎要求的通常需要服务器端代码(例如php)。

Having said that, there are some ways to do this with just JavaScript. 话虽如此,但是有一些方法可以仅使用JavaScript来实现。 The code will be largely browser-specific and generate a security warnings when saving ( as it's typically unsafe to let JavaScript use resources like this ). 该代码将主要针对浏览器,并且在保存时会生成安全警告( 因为让JavaScript使用此类资源通常是不安全的 )。 Specifically, I remember TiddlyWiki , which works with just JavaScript and HTML. 具体来说,我记得TiddlyWiki ,它仅适用于JavaScript和HTML。 Have a look at the code in such a TiddlyWiki file, there's code in there like this: 看一看这样的TiddlyWiki文件中的代码,里面有这样的代码:

// Returns null if it can't do it, false if there's an error, true if it saved OK
function mozillaSaveFile(filePath,content)
{
    if(window.Components) {
        try {
            netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
            var file = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
            file.initWithPath(filePath);
            if(!file.exists())
                file.create(0,0x01B4);// 0x01B4 = 0664
            var out = Components.classes["@mozilla.org/network/file-output-stream;1"].createInstance(Components.interfaces.nsIFileOutputStream);
            out.init(file,0x22,0x04,null);
            out.write(content,content.length);
            out.flush();
            out.close();
            return true;
        } catch(ex) {
            return false;
        }
    }
    return null;
}

As an alternative, depending on the browsers you need to support, you could look into HTML5's local storage capabilities. 或者,根据需要支持的浏览器,可以研究HTML5的本地存储功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何允许人们在我的网站上上传个人资料图片? - How do I allow people to upload profile pictures on my site? 如何修改保存功能以同时保存用户/编辑用户? - How can I modify my save function to do both save user / edit user? 如何允许用户通过我自己的 Google Scripts Web 应用程序编辑我的表格文件? - How do I allow users to edit my Sheets file via my own Google Scripts Web App? 如何生成xml文件并允许用户使用javascript将其保存在本地? - how do I generate an xml file and allow the user to save it locally using javascript? 我的网站上消失了一个区块,我该如何将其恢复? - A block disappeared on my site, how do I bring it back? 如何从响应式 web 站点在 iPhone 上保存文件 - How do I save a file on iPhone from a responsive web site 如何允许我的窗口打开,并且用户输入了文本? - How do I allow my window to open and there be the text the user inputs? 如何将阵列反转回用户输入? - How do I reverse my array back into user input? 如何提示用户将文本(xml)保存到文件? - How do I prompt user to save text (xml) to a file? 当用户访问特定站点时,如何在 Google Chrome 扩展程序中自动保存 URL? - How do I automatically save URLs in a Google Chrome extension when a user visits a specific site?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM