codeigniter redirect after file upload - javascript

1) situation:
On some page the user can use an file upload form to upload a text (or excel) file. After successful upload the user should be redirected to a page where he can do whatever with his file.
2) My controller class:
...
if (!$this->upload->do_upload('fileupload')) {
echo "error file upload not successfull";
}else {
redirect('import/filepreview/');
}
3) what happens
First nothing appears to happen. The file has been uploaded but the page doesn't change.
While using firebug extension it appears there has been sent a Header containing a get request to the proper target url and all the expected content is in the answer. But not displaying on site.
I actually don't know what I am doing wrong.

Solved the problem but only using a workaround.
Finally I used javascript redirection
window.location.replace("http://new.../x_id");
with an x_id which the servers answer to the file upload.
So everything works fine for me. But the original question, why CI's redirect does not work is still open for me.

Related

Clear File List in Fine Uploader in purpose to reupload just one file

I use fine uploader to upload just one file on 'edit entity' page. Entity should have one file assosiated with it. I should show user a popup message with error if he selects more than 1 file in file browser while uploading. I want to set limit to fineUploader and use this setting:
itemLimit: 1
The issue is that if I load one first file it works ok, but if I try to reload it I have validation error (I have my own code for UI that clears control(link) with uploaded file and writes new link there). I know that error appiars because there is a fileList inside Fine Uploader and for it I upload - not reload - another file.
I tryed some code to clear fine uploader state. For example:
onComplete: function (id) {
qq(this.getItemByFileId(id)).remove();
},
But it works only for UI elements, not for "inside" data.
Actually, after several hours googling this issue it seems to me that my problem doesn't have any solution, but probably someone knows...
ok, so, I found the answer. The function that helped me is reset();
It resets the state of fine uploader and file list, sure, also. Now I have validation for case if user select several files for upload in file browser and at the same time I don't have any erros while uploading ONE file several times. Here is my version of 'onAllComplete' event.
onAllComplete: function (succeeded, failed) {
//some code that saves file on the server
this.reset(); // the fix
},

Workaround for Error 414 when opening file from PHP script

I have a PHP script that's outputting a CSV file and up until now I've been just using a link and passing parameters that are used to determine the output in the GET data. However recently the size of the data increased and now that code gets Error 414 - Request URI too Large. I tried using a hidden form to do it with POST but it just reloaded the page and didn't supply a prompt to download the file and all of the suggestions I've been able to find online about doing it with AJAX suggest using a link with GET data instead. Does anyone know a workaround that will have the browser still let the user easily download the data?
Presently I'm just setting the href attribute of a <a> tag.
$("#exportCSV").attr('href', "myscript.php/?data=" + exportData);
exportData has become too long for GET data but I want to maintain the behavior where if you click on a link that has say a CSV file being outputted the browser provides a download dialog for the user.

Upload/download/save image to a local server from a remote server using an Image URL and PHP?

I am building a Discussion Forum as part of a bigger application I am building, the forum is just 1 section of the Application.
For my TextArea fields when posting a new Topic or a Post Reply, I have decided that nothing is as good as the PageDown Markdown Library. It is the same one that StackOverflow uses on all their sites and it works better than many of it's competitors.
The way the library ships though, I am not happy with the default Insert Image functionality. You hit the button to insert an image and it allows you to enter a URL for an Image and then it inserts the proper MarkDown syntax to show the linked image.
This just won't cut it. I need the functionality that you see on StackOverflow! Very similar anyways.
I need it to show a Dialog when you click the Insert Image button, like it does now, but instead of just an input field for a Image URL, it will have 2 filed options...
Upload image from your computer
Insert an Image URL and it will then DOWNLOAD the image from that URL and insert it into the post just as if you had uploaded it from your computer. This is important to not confuse this step. IT should not simply insert the Image linking it to the original Image URL. Instead it will take that URL and download/upload the Image to the same server that the upload from computer option does and then it will insert the NEW Image URL pointing to the newly uploaded image!
Based on some simple HTML like below for a Dialog window with a filed for my Upload from Computer functionality, which I already have working. I need to come up with some JavaScript and PHP that will download/save a remote image to my upload folder on my server when a button is clicked using only the URL that will be inside the URL text input field.
So it will need to do a few things...
Fetch and save an image file to my uploads folder using PHP when the only thing that the PHP function will receive is a URL of the image which could be on the same server or most likely a remote server.
After successfully saving/uploading an image from the URL, the PHP function will return a JSON string with the status/error and if successful then it will also return the actual URL and filename of where the new image is saved on the local server. The JavaScript/AJAX script will receive this JSON response and insert the Markdown syntax for the image into the PageDown editor.
The PHP function will need to ensure that the URL that it is trying to save/download is a valid image file and not some malicious file! Also not simply just some file of the wrong filetype like a non-image file unless we are allowing the file type.
It will be part of a module installed on many dinosaur servers so it needs to work on as many servers as possible too!
From the web
From your computer
I would be greatful of any help, tips, code snippets or anything to help with this. At this stage I really just need to build a nie PHP function that will upload images from a remote URL and also ensure that the URL passed in is a real image file or even better that it is in the allowed file types array!
A couple years ago I had started this but have now lost it and I am starting over and don't remeber much about how I went about doing it then.
The easiest way to download a file from a remote server would be to use copy (http://php.net/manual/en/function.copy.php):
copy('http://someurl.com/image.png', '/var/www/uploads/image.png');
As this function returns a bool, it is easy to determine whether the operation was successful and create a JSON response.
To verify that the file is an actual image, there is unfortunately no way that is 100% sure. It is probably enough to check the mimetype though. You can use finfo for that (http://php.net/manual/en/function.finfo-file.php):
$finfo = finfo_open(FILEINFO_MIME_TYPE);
echo finfo_file($finfo, $filename);
finfo_close($finfo);
For a gif, this would return image/gif for example. You will have to hardcode a list of all mimetypes you want to allow.

POST variables are disabled when action="http://..."

I have a form that points (action) to an url: http://www.mysite.com/actions/process.php?id=12. In this case the post variables are not detected. If I change the url to: actions/process.php?id=12 all works fine.
The big problem is that my form is situated into an include file and the directory of the output files (the pages with the form inside) may change their directory and for this reason I need to use the full url.
But with the full url POST doesn't work...
I forgot to say that the form is submitted by javascript:
document.getElementById('form').submit();
With a normal form it works fine even with a full url...but not if submitted with javascript! I use javascript because of the design of the submit button, I'm forced to do so.
PS: no errors in javascript console
How can I fix it?
PS: I'm working on Ubuntu server with apache installed

window.open(pdffile) generates 404 the requested resource is not available error

I am downloading file from server which is dynamically generated. I am using window.open() function for opening url. I am facing problem. I want to know that whether the file is successfully opened or not. I am using
window.open(url, name, "width=910,height=750,scrollbars=yes");
for opening file but it always opening a window. I want to check whether file is successfully opened or not.
Well, based on the title, it sounds like you're running into what's called a 404 error, or in common English "File Not Found". That means that the path you are supplying to the *.pdf file you want to open is incorrect, ie you're asking the system to open a pdf that doesn't exist.
Since the first argument in window.open() is the Path to the pdf file, look at that variable and fix where it's wrong.

Categories

Resources