使用vbscript classic asp上传图片

时间:2015-05-01 15:52:55

标签: javascript jquery ajax jquery-mobile vbscript

我正在构建我们的移动应用程序的一部分,我试图让点击选择他们选择图片的文件或用他们的相机拿一个。我遇到麻烦之后,用户选择了图片我想将其上传到我们的成像数据库,所以我使用ajax将其发布到asp页面,然后尝试解码base64字符串,然后将其上传到数据库中。我们的数据库只接受二进制数据,我很难完成这一步,任何帮助都会受到赞赏。

html

 <button id="chooseFile" data-theme="b" data-icon="camera">Choose      file</button>
    <div class="hiddenfile">
         <input type="file"  data-clear-btn="false" id="PicFile" name="PicFile" accept="image/*;capture=camera">
    </div>
        <div id="preview">
        </div>
        <ul id="info" data-role="listview" data-inset="true">
        </ul>

    </div>

    <div class="hiddenButton" id="hiddenButton">
        <button id="upload" name="upload" data-theme="b" data-icon="arrow-u" onclick="return PicUpload ()">Upload</button>
        </div>

</div>

将图片构建为基本64字符串的javascript

   var hiddenbutton = $("#hiddenButton");
hiddenbutton.hide();
$('#page').live('pageinit', function(){
    $("#chooseFile").click(function(e){
        e.preventDefault();
        $("input[type=file]").trigger("click");
    });
    $("input[type=file]").change(function(){
        var file = $("input[type=file]")[0].files[0];
        $("#preview").empty();
        displayAsImage3(file, "preview");
        $info = $("#info");
        $info.empty();
        if (file && file.name) {
            $info.append("<li>This is just a preview click the upload button below to upload the picture</li>")
        }
        if (file && file.name) {
            $info.append(hiddenbutton.show());
        }
        $info.listview("refresh");

    });
});

function displayAsImage3(file, containerid) {
    if (typeof FileReader !== "undefined") {
        var container = document.getElementById(containerid),
            img = document.createElement("img"),
            reader;
        container.appendChild(img);
        reader = new FileReader();
        reader.onload = (function (theImg) {
            return function (evt) {
                theImg.src = evt.target.result;
            };
        }(img));
        reader.readAsDataURL(file);
    }




}

我的ajax正在抓取该元素并将其传递给我的asp页面

   function PicUpload () {
    var strDocType;
    var strImage;
    var strPath;
    var strPicExtension;
    var strDOTInfo;
    var strFileType;
    var strFileID;
    var image;
    var imgsomething;




 strDocType = "<%=strDocType%>";
 strImage = ($('img')[0].src);
 strPicExtension = strImage.substring(11, 14)
 strDOTInfo = $("#txtDotInformation").val();
 strFileType = "";
 strFileID = "";
 image = "";
 imgsomething = "";






if (strPicExtension == "jpe") {
    strFileType = "image/jpeg"
}

if (strPicExtension == "gif") {
    strFileType = "image/gif"
}

if (strPicExtension == "png") {
    strFileType = "image/png"
}


if (strPicExtension != "jpe") {
    strPath = strImage.substring(22);
}
else {
    strPath = strImage.substring(23);
}


if (strDOTInfo == "") {
        alert("You must enter the DOT Information")
        $("#txtDotInformation").focus();
        return false;
        }

    if (strDocType == "CALLUPLOAD") {
        var strCallID;
        var strDOTID;
        var strDOTSizeCode;
        var intDOTDateCode;

        strCallID = "<%=strCallID%>";
        strDOTID = strDOTInfo.substring(0,2);
        strDOTSizeCode = strDOTInfo.substring(2,4);
        intDOTDateCode = strDOTInfo.substring(4,8);

        if (strCallID == "") {
        alert("There has to be a call ID")
        return false;
        }


      image =  btoa(strPath);








        jQuery('#pics').html ('<img src="/GYGlobal/images/icons/ajax-  loader.gif"> Uploading Pic . . .');
                jQuery.ajax({
                type: "POST",
                url: "/tools/document_picture_ajax.asp",
                data: 'hidAjaxAction=PicUpload&hidDOTID='+strDOTID+'&hidDOTSizeCode='+strDOTSizeCode+'&hidDOTDateCode='+intDOTDateCode+'&hidCallID='+strCallID+'&hidFileType='+strFileType+'&hidDocType='+strDocType,
                dataType: 'html',
                async: false,
                success: function(data){

                    strFileID = data



                }
            }
        );


        jQuery('#pics').html ('<img src="/GYGlobal/images/icons/ajax-loader.gif"> Uploading Pic . . .');
                jQuery.ajax({
                type: "POST",
                url: "/tools/document_picture_ajax.asp",
                data: 'hidAjaxAction=PicUploadFile&hidFileID='+strFileID+'&hidObjFileData='+image+'&hidDocType='+strDocType,
                dataType: 'html',
                async: false,
                success: function(data){
                    $("#preview").fadeOut("slow");
                    $("#info").fadeOut("slow");
                    AfterPicUpload ()




                }
            }
        );




}

现在我的asp页面代码将数据库中的所有内容和我发现的函数解码为base64字符串

 Case "PicUpload":

Select Case (strDocType)
 Case "CALLUPLOAD":
 Set objImagingFile = New clsImagingIndex
    With objImagingFile

        .intBatchID         = 0
        .strLocation        = gstrLocation
        .strDepartment      = "FleetHQ"
        .strDocumentType    = "Breakdown Photo"
        .strStatus          = "COMPLETE"
        .strKey1                = strCallID
        .strKey2                = strDOTID
        .strKey3                = strDOTSizeCode
        .strKey4                = intDOTDateCode
        .strFileName        = "Call Upload" & " " & strCallID
        .strFileType        = strFileType
        .strAddUser         = gstrUserID
        .strUpdUser         = gstrUserID
        .intFilePageNum = 1
        .Insert()
        intFileID           = .intFileID
        Response.Write intFileID
  End With
  Set objImagingFile = Nothing
  Case "DR":
    Set objImagingFile = New clsImagingIndex
    With objImagingFile
        .intBatchID         = 0
        .strLocation        = gstrLocation
        .strDepartment      = "Sales"
        .strDocumentType    = "Document Photo"
        .strStatus          = "COMPLETE"
        .strKey1                = strLocation
        .strKey2                = strDocType
        .strKey3                = strDocNumber
        .strKey4                = strDocVendor
        .strFileName        = strOriginalFileName
        .strFileType        = strFileType
        .strAddUser         = gstrUserID
        .strUpdUser         = gstrUserID
        .intFilePageNum = 1
        .Insert()
        intFileID           = .intFileID
    End With
    Set objImagingFile = Nothing
Case "IN":
    Set objImagingFile = New clsImagingIndex
    With objImagingFile
        .intBatchID         = 0
         .strLocation       = gstrLocation
        .strDepartment      = "Sales"
        .strDocumentType    = "Document Photo"
        .strStatus          = "COMPLETE"
        .strKey1                = strLocation
        .strKey2                = strDocType
        .strKey3                = strDocNumber
        .strKey4                = strDocVendor
        .strFileName        = strOriginalFileName
        .strFileType        = strFileType
        .strAddUser         = gstrUserID
        .strUpdUser         = gstrUserID
        .intFilePageNum = 1
        .Insert()
        intFileID           = .intFileID
    End With
    Set objImagingFile = Nothing
 End Select

  Case "PicUploadFile":
   Dim objPicData
   Dim objPictureData

  'Response.Write strPath

 objPicData = Base64Decode(strPath)

 Response.Write objPicData

 objPictureData = Base64Decode(objPicData)

 Response.Write objPictureData

    Select Case (strDocType)
    Case "CALLUPLOAD":

    Set objImagingData = New clsImagingData
    With objImagingData
        .intFileID          = intFileID
        .dteDiskExpDate = "90" 'days to add to current date
        .dteDiskExpDate = DateAdd("d",30,Date())
        .objFileData        = objPictureData
        .Insert()
        'Call AddCallPictureCount
    End With
    Set objImagingData = Nothing

    Case "DR":
    Set objImagingData = New clsImagingData
    With objImagingData
        .intFileID          = intFileID
        .dteDiskExpDate = DateAdd("d",30,Date())
        .objFileData        = objFileData
        .Insert()
    Call AddDocumentPictureCount
    End With
    Set objImagingData = Nothing
    Case "IN":
    Set objImagingData = New clsImagingData
    With objImagingData
        .intFileID          = intFileID
        .dteDiskExpDate = "90" 'days to add to current date
        .dteDiskExpDate = DateAdd("d",30,Date())
        .objFileData        = objFileData
        .Insert()
     Call AddDocumentPictureCount
    End With
    Set objImagingData = Nothing
End Select

   Function Base64Decode(ByVal strPath)
   'rfc1521
  '1999 Antonin Foller, Motobit Software, http://Motobit.cz
  Const Base64 =   "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
  Dim dataLength, sOut, groupBegin



 'remove white spaces, If any
 strPath = Replace(strPath, vbCrLf, "")
 strPath = Replace(strPath, vbTab, "")
  strPath = Replace(strPath, " ", "")

  'The source must consists from groups with Len of 4 chars
  dataLength = Len(strPath)
  If dataLength Mod 4 <> 0 Then
   Err.Raise 1, "Base64Decode", "Bad Base64 string."
   Exit Function
 End If


  ' Now decode each group:
  For groupBegin = 1 To dataLength Step 4
   Dim numDataBytes, CharCounter, thisChar, thisData, nGroup, pOut
   ' Each data group encodes up To 3 actual bytes.
   numDataBytes = 3
    nGroup = 0

   For CharCounter = 0 To 3
  ' Convert each character into 6 bits of data, And add it To
  ' an integer For temporary storage.  If a character is a '=', there
  ' is one fewer data byte.  (There can only be a maximum of 2 '=' In
  ' the whole string.)

  thisChar = Mid(strPath, groupBegin + CharCounter, 1)

  If thisChar = "=" Then
    numDataBytes = numDataBytes - 1
    thisData = 0
  Else
    thisData = InStr(1, Base64, thisChar, vbBinaryCompare) - 1
  End If
  If thisData = -1 Then
    Err.Raise 2, "Base64Decode", "Bad character In Base64 string."
    Exit Function
  End If

  nGroup = 64 * nGroup + thisData
   Next

'Hex splits the long To 6 groups with 4 bits
nGroup = Hex(nGroup)


'Add leading zeros
nGroup = String(6 - Len(nGroup), "0") & nGroup

'Convert the 3 byte hex integer (6 chars) To 3 characters
pOut = Chr(CByte("&H" & Mid(nGroup, 1, 2))) + _
  Chr(CByte("&H" & Mid(nGroup, 3, 2))) + _
  Chr(CByte("&H" & Mid(nGroup, 5, 2)))

'add numDataBytes characters To out string
sOut = sOut & Left(pOut, numDataBytes)
  Next

  Base64Decode = sOut
End Function

   Function Base64Decode(ByVal objPicData)
  'rfc1521
  '1999 Antonin Foller, Motobit Software, http://Motobit.cz
 Const Base64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
  Dim dataLength, sOut, groupBegin



 'remove white spaces, If any
 objPicData = Replace(objPicData, vbCrLf, "")
 objPicData = Replace(objPicData, vbTab, "")
  objPicData = Replace(objPicData, " ", "")

'The source must consists from groups with Len of 4 chars
dataLength = Len(objPicData)
 If dataLength Mod 4 <> 0 Then
  Err.Raise 1, "Base64Decode", "Bad Base64 string."
  Exit Function
  End If


 ' Now decode each group:
  For groupBegin = 1 To dataLength Step 4
  Dim numDataBytes, CharCounter, thisChar, thisData, nGroup, pOut
   ' Each data group encodes up To 3 actual bytes.
   numDataBytes = 3
   nGroup = 0

   For CharCounter = 0 To 3
  ' Convert each character into 6 bits of data, And add it To
  ' an integer For temporary storage.  If a character is a '=', there
  ' is one fewer data byte.  (There can only be a maximum of 2 '=' In
  ' the whole string.)

    thisChar = Mid(objPicData, groupBegin + CharCounter, 1)

    If thisChar = "=" Then
    numDataBytes = numDataBytes - 1
    thisData = 0
  Else
    thisData = InStr(1, Base64, thisChar, vbBinaryCompare) - 1
  End If
  If thisData = -1 Then
    Err.Raise 2, "Base64Decode", "Bad character In Base64 string."
    Exit Function
  End If

  nGroup = 64 * nGroup + thisData
   Next

'Hex splits the long To 6 groups with 4 bits
nGroup = Hex(nGroup)

'Add leading zeros
nGroup = String(6 - Len(nGroup), "0") & nGroup

'Convert the 3 byte hex integer (6 chars) To 3 characters
pOut = Chr(CByte("&H" & Mid(nGroup, 1, 2))) + _
  Chr(CByte("&H" & Mid(nGroup, 3, 2))) + _
  Chr(CByte("&H" & Mid(nGroup, 5, 2)))

'add numDataBytes characters To out string
sOut = sOut & Left(pOut, numDataBytes)
  Next

Base64Decode = sOut
End Function

现在,我在javascript中用btoa()方法编码base64string之后运行函数就好了,但是如果我不进行双重编码就不会运行它说坏了base64字符串。我不知道是否需要保存图像,然后将其从文件夹中取出并将其与vbscript一起放入二进制文件中。如果这是我需要做的,我如何从这个base64获取图像并保存到一个.jpeg名称的文件夹?

0 个答案:

没有答案