Sunday, January 17, 2016

qq.FileUploader

https://github.com/Valums-File-Uploader/file-uploader



var uploader = new qq.FileUploader({
        element: document.getElementById('file-uploader-requestDocuments'),
        action: '<%: Url.Action("Create", "RequestDocument") %>',
        params: { id: $('#RequestTempUploadFolderID').val() },
        sizeLimit: 10520000,
        onComplete: function(id, fileName, responseJSON){
            if(!responseJSON.success){alert(responseJSON.errorMessage);}
        }
    });


<div class="modal fade" id="add-File" 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">&times;</button>
                <h4 class="modal-title" id="myModalLabel">تحميل مستند جديد</h4>
            </div>
            <div class="modal-body">
              
                <div class="row">
                    <div class="col-sm-4">
                        <label class="center gray">تحميل المستند</label>
                    </div>
                  
                    <div class="span5" id="DivDocumentUploader">
                        <noscript>
                            <p>
                                enable JavaScript to use file uploader.
                            </p>
                            <!-- or put a simple form for upload here -->
                        </noscript>
                    </div>
                </div>
                <!--####################### upload section ######################################-->
            </div>
        </div>
    </div>
</div>


 ko.applyBindings(owner);

                var FileUpload = new qq.FileUploader({
                    element: document.getElementById('DivDocumentUploader'),
                    action: URL_SelectUploadFile,
                    params: { BookID: owner.BookObj.ID() },
                    allowedExtensions: ['jpg', 'jpeg'],
                    sizeLimit: 125829120,
                    onComplete: function (id, fileName, responseJSON) {

                        $("#loading").hide();
                        $('#fupFileUrl').val(responseJSON.fileName);

                        $('#DivDocumentUploader').next().html('<a class="span" href="#" onclick="window.open(\'' + URL_ViewFile + '?src=' + responseJSON.fileName + '\')"><i class="viewfileicon"></i>ViewFile</a>');
                        owner.RedirecttoEditBook(owner.BookObj.ID());
                    },
                    onSubmit: function (id, fileName) {
                        $("#loading").show();
                    }

                });



                function open_win(url) {
                    alert(url);
                    myWindow = window.open(url, '', 'width=700,height=800')
                }


 #region File Upload
        [HttpGet]
        public FileResult GetImage(string src)
        {
            string fullPath = "~/Content/img/bookimages/" + src;
            string fileExtenstion = Getextension(src);
            string fileType = "image/";
            if (fileExtenstion == "pdf")
            {
                fileType = "application/";
            }

            return File(fullPath, fileType + fileExtenstion);

        }
        private string Getextension(string fileName)
        {
            if (string.IsNullOrEmpty(fileName)) return string.Empty;
            string ext = string.Empty;
            Boolean hita = false;
            int i = fileName.Length - 1;
            char[] arr = fileName.ToCharArray();
            while (i > 0 & !hita)
            {
                if (arr[i] == '.') hita = true;
                else ext = arr[i] + ext;
                i = i - 1;
            }
            return ext;
        }
        [HttpPost]
        public ActionResult SelectUpload(string qqfile,int BookID)
        {
            System.Guid GUID = System.Guid.NewGuid();

            string uniqueFileName = BookID.ToString();//"Temp_" + GUID.ToString();

            string imagesFolder = Request.PhysicalApplicationPath + "\\Content\\img\\bookimages\\";

            if (!System.IO.Directory.Exists(imagesFolder))
                System.IO.Directory.CreateDirectory(imagesFolder);


            var controller = Request.Url.AbsoluteUri;

            var file = string.Empty;
            var url = string.Empty;
            var orginalFileName = string.Empty;
            try
            {
                var stream = Request.InputStream;
                if (String.IsNullOrEmpty(Request["qqfile"]))
                {
                    // IE
                    orginalFileName = Request.Files[0].FileName;
                    uniqueFileName += "." + Getextension(Request.Files[0].FileName);
                    HttpPostedFileBase postedFile = Request.Files[0];
                    stream = postedFile.InputStream;
                    file = System.IO.Path.Combine(imagesFolder, System.IO.Path.GetFileName(uniqueFileName));
                    url = controller.Replace("SelectUpload", "GetImage") + "?src=" + uniqueFileName;
                }
                else
                {
                    //Webkit, Mozilla
                    orginalFileName = qqfile;
                    uniqueFileName += "." + Getextension(qqfile);
                    file = System.IO.Path.Combine(imagesFolder, uniqueFileName);
                    url = controller.Replace("SelectUpload", "GetImage").Replace("?qqfile=" + qqfile, "") + "?src=" + uniqueFileName;
                }
                var buffer = new byte[stream.Length];
                stream.Read(buffer, 0, buffer.Length);
                System.IO.File.WriteAllBytes(file, buffer);
            }
            catch (Exception ex)
            {
                return Json(new { success = false, message = ex.Message }, "application/json");
            }

            // Here call service to change the HasImage to True (17-01-2016)
            serviceUrl += CONST_MSB_UpdateHasImageURL + "/?BookID=" + BookID + "&HasImage=true";
            dynamic UpdateHasImage = MOACommons.HttpMethods.WebAPIHelper.GetWebAPIService(serviceUrl);
            return Json(new { success = true, url = url, fileName = uniqueFileName, sourceFile = qqfile }, "text/html");
        }

        #endregion

No comments:

Post a Comment