Beispiel #1
0
        /// <summary>
        /// 验证是否支持当前文件扩展名
        /// </summary>
        /// <param name="fileName">文件名(带后缀)</param>
        /// <returns>true-支持,false-不支持</returns>
        public bool ValidateFileExtensions(string fileName)
        {
            string fileExtension = fileName.Substring(fileName.LastIndexOf(".") + 1);

            string[] extensions = AllowedFileExtensions.Split(',');

            return(extensions.Where(n => n.Equals(fileExtension, StringComparison.InvariantCultureIgnoreCase)).Count() > 0);
        }
Beispiel #2
0
        /// <summary> Add the file input and the necessary script section, with
        /// all the options specfiedi here, directly to the streamwriter </summary>
        /// <param name="Output"> Writer to write to the stream </param>
        /// <param name="Extra_Indent"> Extra indent to begin each line with (purely for formatting) </param>
        /// <param name="Invalid_Message"> Message to display on fallback, particularly when HTML5 and flash both fail </param>
        public void Add_To_Stream(TextWriter Output, string Extra_Indent, String Invalid_Message)
        {
            if (Version == UploadiFive_Version_Enum.HTML5)
            {
                Output.WriteLine(Extra_Indent + "    $('#" + FileInputID + "').uploadifive({");
            }
            else
            {
                Output.WriteLine(Extra_Indent + "    $('#" + FileInputID + "').uploadify({");
            }


            // Add all the uploadifive options
            if (Auto.HasValue)
            {
                Output.WriteLine(Extra_Indent + "      'auto': " + Auto.Value.ToString().ToLower() + ",");
            }
            if (!string.IsNullOrEmpty(ButtonClass))
            {
                Output.WriteLine(Extra_Indent + "      'buttonClass': '" + ButtonClass + "',");
            }
            if (!string.IsNullOrEmpty(ButtonText))
            {
                Output.WriteLine(Extra_Indent + "      'buttonText': '" + ButtonText + "',");
            }
            if (!string.IsNullOrEmpty(CheckScript))
            {
                Output.WriteLine(Extra_Indent + "      'checkScript': '" + CheckScript + "',");
            }
            if (DragAndDrop.HasValue)
            {
                Output.WriteLine(Extra_Indent + "      'dnd': " + DragAndDrop.Value.ToString().ToLower() + ",");
            }
            if (!string.IsNullOrEmpty(FileObjName))
            {
                Output.WriteLine(Extra_Indent + "      'fileObjName': '" + FileObjName + "',");
            }
            if (!string.IsNullOrEmpty(FileSizeLimit))
            {
                Output.WriteLine(Extra_Indent + "      'fileSizeLimit': '" + FileSizeLimit + "',");
            }
            if (!string.IsNullOrEmpty(FileType))
            {
                Output.WriteLine(Extra_Indent + "      'fileType': '" + FileType + "',");
            }

            // Add the form data
            if (FormData.Count > 0)
            {
                Output.Write(Extra_Indent + "      'formData': { ");
                bool first = true;
                foreach (KeyValuePair <string, string> thisData in FormData)
                {
                    // After the first one, start with the comma seperation
                    if (first)
                    {
                        first = false;
                    }
                    else
                    {
                        Output.Write(", ");
                    }

                    Output.Write("'" + thisData.Key + "' : '" + thisData.Value + "'");
                }
                Output.WriteLine(" },");
            }

            // Finish the uploadifive options
            if (ButtonHeight.HasValue)
            {
                Output.WriteLine(Extra_Indent + "      'height': " + ButtonHeight.Value + ",");
            }
            if (!string.IsNullOrEmpty(ItemTemplate))
            {
                Output.WriteLine(Extra_Indent + "      'itemTemplate': '" + ItemTemplate + "',");
            }
            if (Method == UploadiFive_Method_Enum.Get)
            {
                Output.WriteLine(Extra_Indent + "      'method': 'get',");
            }
            if (Multi.HasValue)
            {
                Output.WriteLine(Extra_Indent + "      'multi': " + Multi.Value.ToString().ToLower() + ",");
            }
            if (!string.IsNullOrEmpty(QueueID))
            {
                Output.WriteLine(Extra_Indent + "      'queueID': '" + QueueID + "',");
            }
            if (QueueSizeLimit.HasValue)
            {
                Output.WriteLine(Extra_Indent + "      'queueSizeLimit': " + QueueSizeLimit.Value + ",");
            }
            if (RemoveCompleted.HasValue)
            {
                Output.WriteLine(Extra_Indent + "      'removeCompleted': " + RemoveCompleted.Value.ToString().ToLower() + ",");
            }
            if (TruncateLength.HasValue)
            {
                Output.WriteLine(Extra_Indent + "      'truncateLength': " + TruncateLength.Value + ",");
            }
            if (UploadLimit.HasValue)
            {
                Output.WriteLine(Extra_Indent + "      'uploadLimit': " + UploadLimit.Value + ",");
            }
            if (ButtonWidth.HasValue)
            {
                Output.WriteLine(Extra_Indent + "      'width': " + ButtonWidth.Value + ",");
            }

            // Add some event handlers
            if (SubmitWhenQueueCompletes)
            {
                Output.WriteLine(Extra_Indent + "      'onQueueComplete': function (uploads) { $('#" + FileInputID + "').closest(\"form\").submit(); },");
            }

            // Is there a file extension restriction here?
            if (!string.IsNullOrEmpty(AllowedFileExtensions))
            {
                // Build the json array of possible file extensions
                string[]      split            = AllowedFileExtensions.Split(",|".ToCharArray());
                StringBuilder jsonArrayBuilder = new StringBuilder(AllowedFileExtensions.Length * 2);
                bool          first            = true;
                foreach (string thisSplit in split)
                {
                    if (first)
                    {
                        jsonArrayBuilder.Append("\"" + thisSplit.Trim().ToLower() + "\"");
                        first = false;
                    }
                    else
                    {
                        jsonArrayBuilder.Append(", \"" + thisSplit.Trim().ToLower() + "\"");
                    }
                }

                // Now, add the event
                if (Version == UploadiFive_Version_Enum.HTML5)
                {
                    Output.WriteLine(Extra_Indent + "      'onAddQueueItem' : function(file) {");
                }
                else
                {
                    Output.WriteLine(Extra_Indent + "      'onSelect' : function(file) {");
                }

                Output.WriteLine(Extra_Indent + "                             var extArray = JSON.parse('[ " + jsonArrayBuilder + " ]');");
                Output.WriteLine(Extra_Indent + "                             var fileName = file.name;");
                Output.WriteLine(Extra_Indent + "                             var ext = fileName.substring(fileName.lastIndexOf('.')).toLowerCase();");
                Output.WriteLine(Extra_Indent + "                             var isExtValid = false;");
                Output.WriteLine(Extra_Indent + "                             for(var i = 0; i < extArray.length; i++) { ");
                Output.WriteLine(Extra_Indent + "                                 if ( ext == extArray[i] ) { isExtValid = true; break; }");
                Output.WriteLine(Extra_Indent + "                             }");

                if (Version == UploadiFive_Version_Enum.HTML5)
                {
                    if (DisallowedFileExtenstionMessage.Length > 0)
                    {
                        Output.WriteLine(Extra_Indent + "                             if ( !isExtValid ) {  alert(\"" + DisallowedFileExtenstionMessage + "\".replace('<extension>', ext)); $('#" + FileInputID + "').uploadifive('cancel', file);  }");
                    }
                    else
                    {
                        Output.WriteLine(Extra_Indent + "                             if ( !isExtValid ) {  $('#" + FileInputID + "').uploadifive('cancel', file);  }");
                    }
                }
                else
                {
                    if (DisallowedFileExtenstionMessage.Length > 0)
                    {
                        Output.WriteLine(Extra_Indent + "                             if ( !isExtValid ) {  alert(\"" + DisallowedFileExtenstionMessage + "\".replace('<extension>', ext)); $('#" + FileInputID + "').uploadify('cancel', '*');  }");
                    }
                    else
                    {
                        Output.WriteLine(Extra_Indent + "                             if ( !isExtValid ) {  $('#" + FileInputID + "').uploadify('cancel', '*');  }");
                    }
                }

                Output.WriteLine(Extra_Indent + "                         },");
            }


            // Set the upload script and finish this
            if (Version == UploadiFive_Version_Enum.HTML5)
            {
                Output.Write(Extra_Indent + "      'uploadScript': '" + UploadScript + "'");
            }
            else
            {
                if (String.IsNullOrEmpty(Swf))
                {
                    Output.WriteLine(Extra_Indent + "      'swf': 'uploadify/uploadify.swf',");
                }
                else
                {
                    Output.WriteLine(Extra_Indent + "      'swf': '" + Swf + "',");
                }

                Output.Write(Extra_Indent + "      'uploader': '" + UploadScript + "'");
            }

            if ((Version == UploadiFive_Version_Enum.HTML5) && (RevertToFlashVersion))
            {
                // ENd the last line, with a paranthesis
                Output.WriteLine(",");
                Output.WriteLine(Extra_Indent + "      'onFallback': function() {");

                Output.WriteLine(Extra_Indent + "                           // Revert to flash version if no HTML5");

                // Switch to SWF version
                Version = UploadiFive_Version_Enum.Flash;
                string buttonCss = ButtonClass;
                if (!String.IsNullOrEmpty(RevertedButtonClass))
                {
                    ButtonClass = RevertedButtonClass;
                }
                try
                {
                    Add_To_Stream(Output, Extra_Indent + "                       ", NoHtml5OrFlashMessage);
                    Output.WriteLine("} //end");
                }
                catch
                {
                    // Just want to ensure the setting is returned
                }
                finally
                {
                    Version     = UploadiFive_Version_Enum.HTML5;
                    ButtonClass = buttonCss;
                }
            }
            else
            {
                if (!String.IsNullOrEmpty(NoHtml5OrFlashMessage))
                {
                    // ENd the last line, with a paranthesis
                    Output.WriteLine(",");
                    Output.WriteLine(Extra_Indent + "      'onFallback': function() { alert('" + NoHtml5OrFlashMessage.Replace("'", "") + "'); }");
                }
                else
                {
                    // End the last line
                    Output.WriteLine();
                }
            }

            Output.WriteLine(Extra_Indent + "    });");
            Output.WriteLine();
        }