将上传的文件信息存入数据库?

时间:2014-12-19 07:40:02

标签: coldfusion coldfusion-10 coldfusion-8 fine-uploader

我使用精细的上传拖放功能上传多张图片。 所以我要求将关于上传图像的信息保存到数据库中:

  1. 创建两个不同大小的图像(大拇指,大拇指)和原始图像。 - (好好创建两个blob文件)
  2. 将单个记录管理到数据库中,以便在上传后保存三种不同大小的单个图像的信息。例如:假设我找到了三个不同的图像 - image_thumbs.jpg,image_large.jpg,image_original.jpg < / strong>将存储在数据库中

    1 | image_thumbs.jpg | image_large.jpg | image_original.jpg

  3. 所以我已经在服务器端设置逻辑以获得图像 qqparentuuid 如果它是子图像/ blob,如果它是父母那么得到 qquuid ,基于UUID(qqparentuuid / qquuid)我从上传的图片得到的,检查数据库是否存在UUID(qqparentuuid / qquuid)然后用当前图像名称更新记录,否则将新记录插入到具有受尊重的UUID(qqparentuuid / qquuid)的数据库中。

    此逻辑工作正常但在少数情况下此逻辑失败,因为上传的文件在服务器上并行上传,有时会在数据库中创建三个或两个不同的行。根据我的观点,当文件在服务器上并行而不是按顺序并且所有逻辑都被触发时,就会发生这种情况。

    我无法弄清楚,这是上传者的问题还是我的最终问题。我在下面分享了我的服务器端逻辑:

    <cffunction name="UploadImages" access="public" output="true" returntype="any">
            <cfargument name="formData" type="struct" required="yes" />
            <cfset var result = true />
            <cfset var temp_Filename = ""/>
            <cfset var local = StructNew() />
            <cfset application.Config.imageDir = "#arguments.formData.imageDir#" />
    
            <!--- This function uploads each image called from FineUploader --->
            <!--- From here, you can call additional processing for resizing, renaming etc. --->
            <cftry>
    
            <!--- Here I rename uploaded files --->
                <cfset fileext = listlast(FORM.qqFileName,".")>
                <cfset filesize=#FORM.QQTOTALFILESIZE#>
                <cfset Rname= "IMG_"&dateFormat(now(), "mmddyyyy")&"_"&TimeFormat(now(), "HHmmssl")&"_"&FORM.QQTOTALFILESIZE>
                <cfset temp_Filename = "#Rname#.#fileext#">
    
    
                <!--- Here uploaded files on server directory--->
                <cffile action="upload"
                destination="#application.Config.imageDir#\#temp_Filename#"
                nameconflict="MAKEUNIQUE" 
                filefield="FORM.qqFile" />
                <cfset local.fileName = CFFILE.serverFile />
    
            <!--- check image qqparentuuid/qquuid on database for existance--->
            <cfquery name="getQuery" maxrows="1" datasource="#application.ds#" dbtype="ODBC" password="#application.password#" username="#application.username#">  
                select * from products where 
                <cfif isDefined("FORM.qqparentuuid")>
                parentuuid=<cfqueryparam value = "#FORM.qqparentuuid#" CFSQLType = "CF_SQL_VARCHAR">
                <cfelse>
                parentuuid=<cfqueryparam value = "#FORM.qquuid#" CFSQLType = "CF_SQL_VARCHAR">
                </cfif>
            </cfquery> 
    
    
    
            <cfif val(getQuery.recordcount) gt 0>
                <!--- if Parentuuid  exist then update records respected UUID--->
    
                <cfquery name="updateQuery" datasource="#application.ds#" dbtype="ODBC" password="#application.password#" username="#application.username#"> 
    
                UPDATE products 
                SET
                <cfif val(FindNoCase("small", FORM.qqFileName)) gt 0>
                thumbimage =  <cfqueryparam value = "#local.fileName#" CFSQLType = "CF_SQL_VARCHAR">,
                <cfelseif val(FindNoCase("medium", FORM.qqFileName)) gt 0>
                fullimage = <cfqueryparam value = "#local.fileName#" CFSQLType = "CF_SQL_VARCHAR">,
                <cfelse>
                masterimage = <cfqueryparam value = "#local.fileName#" CFSQLType = "CF_SQL_VARCHAR">,
                </cfif>
                modifiedBy=<cfqueryparam value = "Artwork admin" CFSQLType = "CF_SQL_VARCHAR">,
                modifiedTs =now()
                WHERE
                <cfif isDefined("FORM.qqparentuuid")>
                parentuuid = <cfqueryparam value = "#FORM.qqparentuuid#" CFSQLType = "CF_SQL_VARCHAR">
                <cfelse>
                parentuuid = <cfqueryparam value = "#FORM.qquuid#" CFSQLType = "CF_SQL_VARCHAR">
                </cfif>
    
            </cfquery>
    
            <cfset productid=#getQuery.productid#>
            <cfelse>
    
    
                <!--- if Parentuuid not exist then insert new one with respect to UUID --->
                <cfquery name="insertQuery" datasource="#application.ds#" dbtype="ODBC" password="#application.password#" username="#application.username#" result="resultid"> 
                INSERT INTO products 
                ( 
                    <cfif val(FindNoCase("small", FORM.qqFileName)) gt 0>
                    thumbimage,
                    <cfelseif val(FindNoCase("medium", FORM.qqFileName)) gt 0>
                    fullimage,
                    <cfelse>
                    masterimage,
                    </cfif>
                    parentuuid,
                    title,
                    createdby, 
                    createdTs
                    )
                VALUES
                (
                    <cfqueryparam value = "#local.fileName#" CFSQLType = "CF_SQL_VARCHAR">,
                    <cfif isDefined("FORM.qqparentuuid")>
                    <cfqueryparam value = "#FORM.qqparentuuid#" CFSQLType = "CF_SQL_VARCHAR">,
                    <cfelse>
                    <cfqueryparam value = "#FORM.qquuid#" CFSQLType = "CF_SQL_VARCHAR">,
                    </cfif>
                    <cfqueryparam value = "Untitled" CFSQLType = "CF_SQL_VARCHAR">,
                    <cfqueryparam value = "Artwork admin" CFSQLType = "CF_SQL_VARCHAR">,
                    now()
                    );
            </cfquery>
    
    
            <cfset productid=#resultid.generated_key#>
    
        </cfif>
    
        <cfset result  = true />
        <cfcatch type="any">
        <cfset result = false />
        <cfrethrow />
        </cfcatch>
    </cftry>
    
    <cfset local.sResult = StructNew() />
    <cfset local.sResult.result = result />
    <cfif result>
        <cfset local.sResult.RETURNID = local.fileName />
        <cfset local.sResult.PRODUCTID = productid />
        <cfif val(FindNoCase("small", FORM.qqFileName)) gt 0>
            <cfset local.sResult.IMAGESIZE = "small" />
            <cfelseif val(FindNoCase("medium", FORM.qqFileName)) gt 0>
                <cfset local.sResult.IMAGESIZE = "medium" />
                <cfelse>
                    <cfset local.sResult.IMAGESIZE = "original" />
                </cfif>
    
                <cfelse>
                    <cfset local.sResult.RETURNOUTPUT = "Place errors here later" />
                </cfif>
    
                <cfreturn local.sResult />
    
            </cffunction>
    

    Fineuploader逻辑:

    $('#fine-uploader').fineUploader({
            // debug: true,
            autoUpload: true,
            scaling: {
                sizes: [{
                    name: "small",
                    maxSize: 150
                }, {
                name: "medium",
                maxSize: 500
            }]
    
        },
        request: {
            endpoint: "Components/fineUploaderProxy.cfm?cfc=ImageDemo&functionName=UploadImages"
        },
        validation: {
            allowedExtensions: ['jpeg', 'jpg', 'png', 'gif'],
        },
        text: {
            uploadButton: 'Choose File'
        }
    })
    .on('progress', function(event, id, name, uploadedBytes, totalBytes) {
        //alert('Name-'+name+"uploadedBytes-" +uploadedBytes+"totalBytes-"+totalBytes);
    
    
    })
    .on('error', function(event, id, name, reason, responseJSON) {
        alert('There was an error with the upload.' + reason);
    })
    
    .on('complete', function(event, id, name, responseJSON) {
    
    
        if (responseJSON.success) {
            if (responseJSON.HASERRORS == true) {
                $('.qq-upload-success').hide();
                alert(responseJSON.DATA.RETURNOUTPUT);
            } else {
                $('.qq-upload-success').hide();
    
                if (responseJSON.IMAGESIZE == 'small') {
                    ProductObjList.push(responseJSON.PRODUCTID);
                    $('#outputImage').append('<li><a onclick="setArtDetailsURL(' + responseJSON.PRODUCTID + ');" class="thumbClass" id=' + responseJSON.PRODUCTID + '><img height="130" src="images/' + responseJSON.RETURNID + '" alt="" /></a></li>');
    
    
    
                }
    
            }
        }
    })
    .on('allComplete', function(event, succeeded, failed) {
        /* set final list of product after complete image upload*/
        $('#productlist').val(ProductObjList.toString());
    })
    

    所以请建议。

    由于

0 个答案:

没有答案