Posts hayageek - jquery-upload-file
Post
Cancel

hayageek - jquery-upload-file

reference - http://github.com/hayageek/jquery-upload-file

seems that any error occured at event

1
onError: function(files,status,errMsg,pd)

auto handled by library and shown at user when the showError=true (by default)

when raising errors by your php script the only way to catch it is by

1
onSuccess:function(files,data,xhr,pd)

index.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
	var uploadObj;

	$(function()
		{

			//when modal closed, hide the warning messages + reset
			$('#modalOFFERS2').on('hidden.bs.modal', function()
				{
					//when close - clear elements
					$('#formOFFERS2').trigger("reset");

					//remove any status groupboxes from jQ-uploader via class!
					$(".ajax-file-upload-statusbar").remove();
				});

			$('#btn_lead_proposals_new').on('click', function(e)
				{
					e.preventDefault();
					$('#modalOFFERS2').modal('toggle');
				})

			uploadObj = $("#file_prop_approval").uploadFile(
				{
					url:"upload.php",
					showProgress : true,
					fileName:"myfile",
					autoSubmit:true,
					maxFileCount:1,
					maxFileSize:31457280, //30mb
					dynamicFormData: function()
					{
					   //send JSON variables with $FILE
					   var data ={ client_id : <?= $_GET['id'] ?>, offer_id: $("#OFFERS2FORM_updateID").val() };
					   return data;        
					},
					onSubmit:function(files)
					{//callback to be invoked before the file upload.

						//reset jQ-uploader counters!
						console.log("refresh")
						uploadObj.fileCounter = 0;
						uploadObj.selectedFiles = 0;
						uploadObj.fCounter = 0; //failed uploads
						uploadObj.sCounter = 0; //success uploads
						uploadObj.tCounter = 0; //total uploads

						if (!files)
						return;
					},
					onSuccess:function(files,data,xhr,pd)
					{
						//custom error handling
						var info = JSON.parse(data);

						if (info["jquery-upload-file-error"]!=null)
						{	
                                                    //show the error thrown by upload PHP
                                                    alert(info["jquery-upload-file-error"]);

                                                    //remove any status groupboxes from jQ-uploader via class!
                                                    $(".ajax-file-upload-statusbar").remove();

                                                    //reset jQ-uploader counters!
                                                    console.log("refresh")
                                                    uploadObj.fileCounter = 0;
                                                    uploadObj.selectedFiles = 0;
                                                    uploadObj.fCounter = 0; //failed uploads
                                                    uploadObj.sCounter = 0; //success uploads
                                                    uploadObj.tCounter = 0; //total uploads
						}
					},
					afterUploadAll:function(obj)
					{
						//callback after upload
					}

				});

		}); //jQuery ends

<button id="btn_lead_proposals_new" class="btn btn-success" type="submit" name="submit">
	New
</button>

<div class="modal fade" id="modalOFFERS2" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-hidden="true">
					×
				</button>

#### 
					New

			</div>
			<div class="modal-body">
				<form id="formOFFERS2" role="form">

					<button id="btn_dn_file_prop_approval" type="button" class="btn btn-primary">
						Download
					</button>

					<div id="file_prop_approval">
						Upload
					</div>

					<input name="OFFERS2FORM_updateID" id="OFFERS2FORM_updateID" class="form-control" style="display:none;">

					<div class="modal-footer">
						<button id="bntCancel_OFFERS2" type="button" class="btn btn-default" data-dismiss="modal">
							back
						</button>
					</div>
				</form>
			</div>
		</div>
	</div>
</div>

upload.php

1
<?php session_start();="" if="" (!isset($_session["u"])="" ||="" empty($_post['offer_id'])="" ||="" empty($_post['client_id']))="" {="" drives="" to="" library="" onerror="" event="" (aka="" shows="" the="" error="" automatically="" at="" groupbox)="" header($_server['server_protocol']="" .="" '="" 500="" internal="" server="" error',="" true,="" 500);="" exit="" ;="" }="" $company_id="$_POST['client_id'];" $output_dir="./destdir/$company_id/" ;="" $filename="$_POST['offer_id']." _test.tiff";"="" raise="" custom="" error="" if="" (file_exists($output_dir.$filename))="" {="" custom="" error,="" we="" catch="" it="" at="" **onsuccess**="" event="" $ret="array();" $ret['jquery-upload-file-error']='Approval file already exists!' ;="" echo="" json_encode($ret);="" exit;="" }="" if(isset($_files["myfile"]))="" {="" $ret="array();" $error="$_FILES[" myfile"]["error"];"="" you="" need="" to="" handle="" both="" cases="" if="" any="" browser="" does="" not="" support="" serializing="" of="" multiple="" files="" using="" formdata()="" if(!is_array($_files["myfile"]["name"]))="" single="" file="" {="" move_uploaded_file($_files["myfile"]["tmp_name"],$output_dir.$filename);="" $ret[]="$fileName;" }="" else="" multiple="" files,="" file[]="" {="" $filecount="count($_FILES[" myfile"]["name"]);"="" for($i="0;" $i=""?>< $filecount;="" $i++)="" {="" $filename="$_FILES[" myfile"]["name"][$i];"="" move_uploaded_file($_files["myfile"]["tmp_name"][$i],$output_dir.$filename);="" $ret[]="$fileName;" }="" }="" echo="" json_encode($ret);="" }="">

//catch the done button, using jQuery dynamic event

1
2
3
4
$(document).on("click", ".ajax-file-upload-green", function(e) {
	if (upload_form=="modalOFFERS2")
		$('#modalOFFER').modal('toggle');
	})

origin - http://www.pipiscrew.com/?p=1485 hayageek-jquery-upload-file

This post is licensed under CC BY 4.0 by the author.
Contents

Trending Tags