diff --git a/App/Controllers/Jurisdiction/JurisdictionController.asp b/App/Controllers/Jurisdiction/JurisdictionController.asp index 6baaa2c..2981170 100644 --- a/App/Controllers/Jurisdiction/JurisdictionController.asp +++ b/App/Controllers/Jurisdiction/JurisdictionController.asp @@ -150,6 +150,42 @@ Class JurisdictionController %> <% End sub + + Public Sub Import + + dim page_size : page_size = 10 + dim page_num : page_num = Choice(Len(Request.Querystring("page_num")) > 0, Request.Querystring("page_num"), 1) + dim page_count, record_count + set Model = new PagedIndex_ViewModel_Class + Model.Title = "Jurisdiction" + set Model.Jurisdiction = JurisdictionRepository.FindPaged(empty, "JCode", page_size, page_num, page_count, record_count) + Model.CurrentPageNumber = page_num + Model.PageSize = page_size + Model.PageCount = page_count + 'Model.RecordCount = record_count + %> <% + + End Sub + + Public Sub ImportPost + + Dim Upload:Set Upload = New FreeASPUpload + Upload.Save(server.mappath("/uploads")) + Chilkat_CSV.LoadFile(Upload.UploadedFiles("filename").path) + set Model = new PagedIndex_ViewModel_Class + + Model.RecordCount = Chilkat_CSV.NumRows + Dim RowNumber + On Error Resume Next + for RowNumber = 1 to Model.RecordCount + + Dim Record: Set Record = JurisdictionRepository.FindByJCode(Chilkat_CSV.GetCell(CLng(RowNumber),0)) + + Next + On Error Goto 0 + %> <% + + End Sub End Class MVC.Dispatch %> diff --git a/App/Views/Jurisdiction/import.asp b/App/Views/Jurisdiction/import.asp new file mode 100644 index 0000000..80f52cc --- /dev/null +++ b/App/Views/Jurisdiction/import.asp @@ -0,0 +1,23 @@ +
This will delete all records and import the new records from the file
Records in the CSV: <%= Model.RecordCount %>
+<% End If %> +" + response.write Err.Description & "
" + response.write "The most likely cause for this error is the incorrect setup of AspMaxRequestEntityAllowed in IIS MetaBase. Please see instructions in the requirements page of freeaspupload.net.
" + Exit Sub + end if + on error goto 0 'reset error handling + + nCurPos = FindToken(tNewLine,1) 'Note: nCurPos is 1-based (and so is InstrB, MidB, etc) + + If nCurPos <= 1 Then Exit Sub + + 'vDataSep is a separator like -----------------------------21763138716045 + vDataSep = MidB(VarArrayBinRequest, 1, nCurPos-1) + + 'Start of current separator + nDataBoundPos = 1 + + 'Beginning of last line + nLastSepPos = FindToken(vDataSep & tTerm, 1) + + Do Until nDataBoundPos = nLastSepPos + + nCurPos = SkipToken(tContentDisp, nDataBoundPos) + nCurPos = SkipToken(tName, nCurPos) + sFieldName = ExtractField(tDoubleQuotes, nCurPos) + + nPosFile = FindToken(tFilename, nCurPos) + nPosBound = FindToken(vDataSep, nCurPos) + + If nPosFile <> 0 And nPosFile < nPosBound Then + Dim oUploadFile + Set oUploadFile = New UploadedFile + + nCurPos = SkipToken(tFilename, nCurPos) + auxStr = ExtractField(tDoubleQuotes, nCurPos) +' We are interested only in the name of the file, not the whole path +' Path separator is \ in windows, / in UNIX +' While IE seems to put the whole pathname in the stream, Mozilla seem to +' only put the actual file name, so UNIX paths may be rare. But not impossible. +osPathSep = "\" +if InStr(auxStr, osPathSep) = 0 then osPathSep = "/" + oUploadFile.FileName = Right(auxStr, Len(auxStr)-InStrRev(auxStr, osPathSep)) + + if (Len(oUploadFile.FileName) > 0) then 'File field not left empty + nCurPos = SkipToken(tContentType, nCurPos) + +auxStr = ExtractField(tNewLine, nCurPos) +' NN on UNIX puts things like this in the stream: +' ?? python py type=?? python application/x-python + oUploadFile.ContentType = Right(auxStr, Len(auxStr)-InStrRev(auxStr, " ")) + nCurPos = FindToken(tNewLine, nCurPos) + 4 'skip empty line + + oUploadFile.Start = nCurPos+1 + oUploadFile.Length = FindToken(vDataSep, nCurPos) - 2 - nCurPos + + If oUploadFile.Length > 0 Then UploadedFiles.Add LCase(sFieldName), oUploadFile + End If + Else + Dim nEndOfData + nCurPos = FindToken(tNewLine, nCurPos) + 4 'skip empty line + nEndOfData = FindToken(vDataSep, nCurPos) - 2 + If Not FormElements.Exists(LCase(sFieldName)) Then + FormElements.Add LCase(sFieldName), Byte2String(MidB(VarArrayBinRequest, nCurPos, nEndOfData-nCurPos)) + else +FormElements.Item(LCase(sFieldName))= FormElements.Item(LCase(sFieldName)) & ", " & Byte2String(MidB(VarArrayBinRequest, nCurPos, nEndOfData-nCurPos)) +end if + + End If + + 'Advance to next separator + nDataBoundPos = FindToken(vDataSep, nCurPos) + Loop + StreamRequest.WriteText(VarArrayBinRequest) + End Sub + + Private Function SkipToken(sToken, nStart) + SkipToken = InstrB(nStart, VarArrayBinRequest, sToken) + If SkipToken = 0 then + Response.write "Error in parsing uploaded binary request. The most likely cause for this error is the incorrect setup of AspMaxRequestEntityAllowed in IIS MetaBase. Please see instructions in the requirements page of freeaspupload.net.
" + Response.End + end if + SkipToken = SkipToken + LenB(sToken) + End Function + + Private Function FindToken(sToken, nStart) + FindToken = InstrB(nStart, VarArrayBinRequest, sToken) + End Function + + Private Function ExtractField(sToken, nStart) + Dim nEnd + nEnd = InstrB(nStart, VarArrayBinRequest, sToken) + If nEnd = 0 then + Response.write "Error in parsing uploaded binary request." + Response.End + end if + ExtractField = Byte2String(MidB(VarArrayBinRequest, nStart, nEnd-nStart)) + End Function + + 'String to byte string conversion + Private Function String2Byte(sString) + Dim i + For i = 1 to Len(sString) + String2Byte = String2Byte & ChrB(AscB(Mid(sString,i,1))) + Next + End Function + + 'Byte string to string conversion + Private Function Byte2String(bsString) + Dim i + dim b1, b2, b3, b4 + Byte2String ="" + For i = 1 to LenB(bsString) + if AscB(MidB(bsString,i,1)) < 128 then + ' One byte + Byte2String = Byte2String & ChrW(AscB(MidB(bsString,i,1))) + elseif AscB(MidB(bsString,i,1)) < 224 then + ' Two bytes + b1 = AscB(MidB(bsString,i,1)) + b2 = AscB(MidB(bsString,i+1,1)) + Byte2String = Byte2String & ChrW((((b1 AND 28) / 4) * 256 + (b1 AND 3) * 64 + (b2 AND 63))) + i = i + 1 + elseif AscB(MidB(bsString,i,1)) < 240 then + ' Three bytes + b1 = AscB(MidB(bsString,i,1)) + b2 = AscB(MidB(bsString,i+1,1)) + b3 = AscB(MidB(bsString,i+2,1)) + Byte2String = Byte2String & ChrW(((b1 AND 15) * 16 + (b2 AND 60)) * 256 + (b2 AND 3) * 64 + (b3 AND 63)) + i = i + 2 + else + ' Four bytes + b1 = AscB(MidB(bsString,i,1)) + b2 = AscB(MidB(bsString,i+1,1)) + b3 = AscB(MidB(bsString,i+2,1)) + b4 = AscB(MidB(bsString,i+3,1)) + ' Don't know how to handle this, I believe Microsoft doesn't support these characters so I replace them with a "^" + 'Byte2String = Byte2String & ChrW(((b1 AND 3) * 64 + (b2 AND 63)) & "," & (((b1 AND 28) / 4) * 256 + (b1 AND 3) * 64 + (b2 AND 63))) + Byte2String = Byte2String & "^" + i = i + 3 + end if + Next + End Function +End Class + +Class UploadedFile + Public ContentType + Public Start + Public Length + Public Path + Private nameOfFile + +' Need to remove characters that are valid in UNIX, but not in Windows +Public Property Let FileName(fN) +nameOfFile = fN +nameOfFile = SubstNoReg(nameOfFile, "\", "_") +nameOfFile = SubstNoReg(nameOfFile, "/", "_") +nameOfFile = SubstNoReg(nameOfFile, ":", "_") +nameOfFile = SubstNoReg(nameOfFile, "*", "_") +nameOfFile = SubstNoReg(nameOfFile, "?", "_") +nameOfFile = SubstNoReg(nameOfFile, """", "_") +nameOfFile = SubstNoReg(nameOfFile, "<", "_") +nameOfFile = SubstNoReg(nameOfFile, ">", "_") +nameOfFile = SubstNoReg(nameOfFile, "|", "_") +End Property + +Public Property Get FileName() +FileName = nameOfFile +End Property + +'Public Property Get FileN()ame +End Class + + +' Does not depend on RegEx, which is not available on older VBScript +' Is not recursive, which means it will not run out of stack space +Function SubstNoReg(initialStr, oldStr, newStr) +Dim currentPos, oldStrPos, skip +If IsNull(initialStr) Or Len(initialStr) = 0 Then +SubstNoReg = "" +ElseIf IsNull(oldStr) Or Len(oldStr) = 0 Then +SubstNoReg = initialStr +Else +If IsNull(newStr) Then newStr = "" +currentPos = 1 +oldStrPos = 0 +SubstNoReg = "" +skip = Len(oldStr) +Do While currentPos <= Len(initialStr) +oldStrPos = InStr(currentPos, initialStr, oldStr) +If oldStrPos = 0 Then +SubstNoReg = SubstNoReg & Mid(initialStr, currentPos, Len(initialStr) - currentPos + 1) +currentPos = Len(initialStr) + 1 +Else +SubstNoReg = SubstNoReg & Mid(initialStr, currentPos, oldStrPos - currentPos) & newStr +currentPos = oldStrPos + skip +End If +Loop +End If +End Function + +Function GetFileName(strSaveToPath, FileName) +'This function is used when saving a file to check there is not already a file with the same name so that you don't overwrite it. +'It adds numbers to the filename e.g. file.gif becomes file1.gif becomes file2.gif and so on. +'It keeps going until it returns a filename that does not exist. +'You could just create a filename from the ID field but that means writing the record - and it still might exist! +'N.B. Requires strSaveToPath variable to be available - and containing the path to save to +Dim Counter +Dim Flag +Dim strTempFileName +Dim FileExt +Dim NewFullPath +dim objFSO, p +Set objFSO = CreateObject("Scripting.FileSystemObject") +Counter = 0 +p = instrrev(FileName, ".") +FileExt = mid(FileName, p+1) +strTempFileName = left(FileName, p-1) +NewFullPath = strSaveToPath & "\" & FileName +Flag = False + +Do Until Flag = True +If objFSO.FileExists(NewFullPath) = False Then +Flag = True +GetFileName = Mid(NewFullPath, InstrRev(NewFullPath, "\") + 1) +Else +Counter = Counter + 1 +NewFullPath = strSaveToPath & "\" & strTempFileName & Counter & "." & FileExt +End If +Loop +End Function +%> \ No newline at end of file diff --git a/MVC/lib.all.asp b/MVC/lib.all.asp index d7b6fde..d70f189 100644 --- a/MVC/lib.all.asp +++ b/MVC/lib.all.asp @@ -12,4 +12,6 @@ - \ No newline at end of file + + + \ No newline at end of file