javascript 以编程方式将文件上传到 ownCloud 服务器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24758167/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Uploading files to an ownCloud server programmatically
提问by tampeta
I am trying to set a web application where many clients can connect through a Node.js http server and then upload/download files that will then be shown in different displays. I am thinking about having those files stored in a free cloud service that can be integrated to my app. Oh, and I am also using socket.IO in this project.
我正在尝试设置一个 Web 应用程序,其中许多客户端可以通过 Node.js http 服务器进行连接,然后上传/下载文件,然后这些文件将显示在不同的显示器中。我正在考虑将这些文件存储在可以集成到我的应用程序中的免费云服务中。哦,我也在这个项目中使用了 socket.IO。
Dropbox offers some API to do this: https://www.dropbox.com/developersbut I was looking into a free solution like ownCloud where I can have a larger amount of storage and also have my own private server.
Dropbox 提供了一些 API 来做到这一点:https: //www.dropbox.com/developers,但我正在寻找像 ownCloud 这样的免费解决方案,在那里我可以拥有更大的存储量并拥有自己的私人服务器。
Does anyone know if this can be done? or can offer any tips about alternative solutions to my problem? I would really appreciate any help with this since I am quite new to all this.
有谁知道这是否可以做到?或者可以为我的问题提供有关替代解决方案的任何提示?我真的很感激这方面的任何帮助,因为我对这一切都很陌生。
回答by Frank Lu
@Javier Gonzalez, To upload a file...
@Javier Gonzalez,要上传文件...
Bad option:
错误的选择:
curl -X PUT "http://yourserver.com/owncloud/remote.php/webdav/file.zip" -F myfile=@"/Users/Javi/Downloads/file.zip"
For, the uploaded file will contain the http headers.
因为,上传的文件将包含 http 标头。
Better option:
更好的选择:
curl -X PUT "http://yourserver.com/owncloud/remote.php/webdav/file.zip" --data-binary @"/Users/Javi/Downloads/file.zip"
Or just use curl -T filename url to upload
或者直接使用 curl -T filename url 上传
回答by Javier Gonzalez
You have to communicate with the WebDav interface at http://yourowncloudserver.com/owncloud/remote.php/webdav/
您必须在http://yourowncloudserver.com/owncloud/remote.php/webdav/ 上与 WebDav 界面进行通信
To upload a file you have to make a PUT request to the destiny of the file for example: http://yourowncloudserver.com/owncloud/remote.php/webdav/file.zip
要上传文件,您必须向文件的命运发出 PUT 请求,例如:http: //yourowncloudserver.com/owncloud/remote.php/webdav/file.zip
And add the input stream to the request to read the file.
并将输入流添加到读取文件的请求中。
Here is the CURL command:
这是 CURL 命令:
curl -X PUT -u username:password "http://yourserver.com/owncloud/remote.php/webdav/file.zip" -F myfile=@"/Users/Javi/Downloads/file.zip"
You also can check our code on Objective C to check the parameters that we use: ownCloud iOS Library
您还可以检查我们在 Objective C 上的代码以检查我们使用的参数: ownCloud iOS 库
NSMutableURLRequest *request = [self requestWithMethod:@"PUT" path:remoteDestination parameters:nil];
[request setTimeoutInterval:k_timeout_upload];
[request setValue:[NSString stringWithFormat:@"%lld", [UtilsFramework getSizeInBytesByPath:localSource]] forHTTPHeaderField:@"Content-Length"];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setHTTPShouldHandleCookies:NO];
//[request setHTTPBody:[NSData dataWithContentsOfFile:localSource]];
__weak __block OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request success:success failure:failure];
[operation setInputStream:[NSInputStream inputStreamWithFileAtPath:localSource]];
回答by Johannes Stadler
As an addition to the existing answers, I use the following function as alias:
作为对现有答案的补充,我使用以下函数作为别名:
cloud() {
curl -X PUT -u "<username>:<password>" "https://<hostname>/remote.php/webdav/${2:-}" --data-binary @"./"
}
Just replace <username>
, <password>
and <hostname>
and put this in your .bash_aliases and you can upload your files with:
只需替换<username>
,<password>
并将<hostname>
其放入您的 .bash_aliases 中,您就可以上传您的文件:
cloud filename
cloud path filename
回答by bartv
The easiest way would be to use the webdav interface of owncloud
最简单的方法是使用 owncloud 的 webdav 界面
回答by Fabrice Courtaud
Using curl for windows and owncloud 8. The only way I found to transfer a file was by using this command
在 windows 和 owncloud 8 上使用 curl。我发现传输文件的唯一方法是使用此命令
curl -u user:password --upload-file "c:\path to file\srcfile" "https://ocserver/owncloud/remote.php/webdav/tgtfile"
Hope this helps
希望这可以帮助
回答by Gokul
Upload a fileyou have to make a PUTrequest to the destiny of the file for example: http://yourOwnCloudServer/remote.php/webdav/text.txt
上传一个你必须向文件的命运发出PUT请求的文件,例如:http://yourOwnCloudServer/remote.php/webdav/text.txt
Here is the CURL command:
这是 CURL 命令:
curl -X PUT -u username:password "http://yourOwnCloudServer/remote.php/webdav/text.txt" -F myfile=@"/Users/gokul/Desktop/text.txt"
You can also use --data-binary
for media files.
您还可以--data-binary
用于媒体文件。
"http://yourOwnCloudServer/remote.php/webdav/image.jpg" --data-binary @"/Users/gokul/Desktop/image.jpg"