javascript jQuery Blueimp FileUpload 上的 DELETE 方法不允许使用 Codeigniter/PHP(405 错误)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11870789/
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
DELETE method on jQuery Blueimp FileUpload not allowed using Codeigniter/PHP (405 error)?
提问by tim peterson
I'm trying to delete files using this very nice jQuery Blueimp File Upload plugin.
我正在尝试使用这个非常好的jQuery Blueimp 文件上传插件来删除文件。
I put this plugin in my root directory and was able to upload and delete files no problem.
我把这个插件放在我的根目录下,上传和删除文件没问题。
However, when I embed this plugin within my codeigniter app I'm no longer to delete files I've uploaded due to a 405 error. I've set all the folders to 777
just to make sure that isn't an issue.
但是,当我在我的 codeigniter 应用程序中嵌入这个插件时,我不再删除由于 405 错误而上传的文件。我已将所有文件夹设置为777
只是为了确保这不是问题。
Any thoughts? Here's my console log:
有什么想法吗?这是我的控制台日志:
采纳答案by tim peterson
I solved my own problem following the code in one of the Codeigniter Forksof this Blueimp plugin.
我按照此 Blueimp 插件的 Codeigniter Forks 之一中的代码解决了我自己的问题。
The problem was the URL of the DELETE HTTP/AJAX request that the Blueimp plugin specifies by default. Those URLs correspond to a directory path of where the file is uploaded. Unfortunately, Codeigniter by default overrides this by using the URL to determine what controller/controller_method to call.
问题是 Blueimp 插件默认指定的 DELETE HTTP/AJAX 请求的 URL。这些 URL 对应于文件上传位置的目录路径。不幸的是,默认情况下,Codeigniter 通过使用 URL 来确定要调用的控制器/控制器方法来覆盖它。
So for example, my directory structure for the uploaded file is this:
例如,我上传文件的目录结构是这样的:
/uploads/img1.jpg
and Codeigniter looked for a controller called uploads
and a method called img1.jpg
but those obviously didn't exist.
和 Codeigniter 寻找一个被调用的控制器uploads
和一个被调用的方法,img1.jpg
但这些显然不存在。
I solved this by changing the Blueimp plugin "upload.class.php" file delete_url
that gets assigned to each file. The delete_url
was changed from a directory location to a codeigniter controller/controller_method as follows:
我通过更改delete_url
分配给每个文件的 Blueimp 插件“upload.class.php”文件解决了这个问题 。在delete_url
从一个目录位置改变到笨控制器/ controller_method如下:
protected function set_file_delete_url($file) {
$file->delete_url = base_url().'upload/deleteFile/'.rawurlencode($file->name);
//"upload/deleteFile is my controller/controller_method
//$file->delete_url = $this->options['upload_url']
// .'?file='.rawurlencode($file->name);*/
//.....
and then here is what my upload/deleteFile
function looks like (again following the code nearly verbatim in the Codeigniter Blueimp Fork):
然后这是我的upload/deleteFile
函数的样子(再次遵循Codeigniter Blueimp Fork 中几乎逐字逐句的代码):
function deleteFile($file){
$fcpath=FCPATH.'uploads/;
$success =unlink($fcpath.$file); //PHP function was does the actual file deletion
//info to see if it is doing what it is supposed to
$info->sucess =$success;
$info->file =is_file(FCPATH .$file);
$info->fcpath = FCPATH;
if (IS_AJAX) {
//I don't think it matters if this is set but good for error checking in the console/firebug
echo json_encode(array($info));
}
else {
//here you will need to decide what you want to show for a successful delete
$file_data['delete_data'] = $file;
$this->load->view('admin/delete_success', $file_data);
}
}
回答by genarocupil
You should only modify this file UploadHandler.php:
你应该只修改这个文件 UploadHandler.php:
function __construct($options = null, $initialize = true, $error_messages = null) {
// Set the following option to 'POST', if your server does not support
// DELETE requests. This is a parameter sent to the client:
//'delete_type' => 'DELETE',
'delete_type' => 'POST',
function __construct($options = null, $initialize = true, $error_messages = null) {
// 将以下选项设置为 'POST',如果您的服务器不支持
// DELETE 请求。这是发送给客户端的参数:
//'delete_type' => 'DELETE',
'delete_type' => 'POST',
回答by Jan Pierre Sanchez
I solved it, in the following way:
我通过以下方式解决了它:
in the file: UploadHandler.php, in the constructor
在文件:UploadHandler.php 中,在构造函数中
Before:
前:
$this->options = array(
'script_url' => $this->get_full_url().'/'.$this->basename($this->get_server_var('SCRIPT_NAME')),
Add to the end my load control method
最后加上我的负载控制方法
$this->options = array(
'script_url' => $this->get_full_url().'/'.$this->basename($this->get_server_var('SCRIPT_NAME')).'/cargar'
Just add the route pointing to the method you created to upload your files.
只需添加指向您创建的上传文件的方法的路由。
回答by Felipe Maia
I added the jquery file upload to a controller in my application. Like Mark, I could not get the files deleted. But I solved the problem like this:
我将 jquery 文件上传添加到我的应用程序中的控制器。像马克一样,我无法删除文件。但我解决了这样的问题:
In the constructor, the first element of the optionsarray
在构造函数中,options数组的第一个元素
'script_url' => $this->get_full_url().this->basename($this->get_server_var('SCRIPT_NAME')),
remove the reference to SCRIPT_FILENAME
删除对 SCRIPT_FILENAME 的引用
remove the reference to SCRIPT_FILENAME and add the name of the driver that loads the uploadhandler.phplibrary. In my case, fileupload
删除对 SCRIPT_FILENAME 的引用并添加加载uploadhandler.php库的驱动程序的名称。就我而言,文件上传
'script_url' => $this->get_full_url().'/fileupload/',
Ready. Just try!
准备好。你试一试!
回答by Talha Masood
All you need to do is change the "script_url" inside "UploadHandler" and point it to where your "UploadHandler" library is called.
您需要做的就是更改“UploadHandler”中的“script_url”并将其指向调用“UploadHandler”库的位置。
//BEFORE
//前
$this->options = array(
'script_url'=>$this->get_full_url().'/'.$this->basename($this->get_server_var('SCRIPT_NAME')),
//AFTER
//后
$this->options = array(
'script_url'=>base_url().'admin/categories/category_manage/picturesupload'
Because i am using bonfire-CI thats why
"admin" for admin panel
"categories" is the name of context
"category_manage" is the name of module
"picturesupload" is the name of method in which i am loading the libarary
If you are not using bonfire then simply change it to "your controller name"/"your method name where the UploadHandler libaray is called"
//"picturesupload" method look like this
因为我正在使用 bonfire-CI 这就是为什么管理面板
“类别”的“ admin ”是上下文的名称
“category_manage”是模块的名称
“picturesupload”是我加载库的方法的名称
如果你不是使用篝火,然后只需将其更改为“您的控制器名称”/“您的方法名称,其中调用 UploadHandler libaray”
//“picturesupload”方法如下所示
public function picturesupload(){
error_reporting(E_ALL | E_STRICT);
$this->load->library('UploadHandler');
}