1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
| Private Sub Uploadfile(ByVal context As HttpContext)
context.Response.ContentType = "text/plain"
Dim r As New Generic.LinkedList(Of ViewDataUploadFilesResult)
Dim js As New Script.Serialization.JavaScriptSerializer
If context.Request.Files.Count >= 1 Then
If Not System.IO.Directory.Exists(RootFolder) Then System.IO.Directory.CreateDirectory(RootFolder)
Dim InfNav As HelperBrowser.InfoNavigateur = HelperBrowser.InfoBrowser
Dim FileName As String = String.Empty
Dim hpf As HttpPostedFile = Nothing
Dim Range = context.Request.Headers.GetValues("Content-Range")
For Each file As String In context.Request.Files
hpf = context.Request.Files(file)
If InfNav.Name = HelperBrowser.IE Then
Dim files As String() = hpf.FileName.Split(CChar("\\"))
FileName = files(files.Length - 1)
Else
FileName = Path.GetFileName(hpf.FileName)
End If
Dim savedFileName As String = RootFolder & FileName
Try
'Fichier envoyé en 1 seul bloc
'Vérif taille : sinon chunck
If Range Is Nothing Then
If hpf.ContentLength <= CDbl(HelperParams.GetConfig(PARAM_UPL_JS_SIZE_MAX_FILE)) Then
If CDbl(hpf.ContentLength + CDbl(HelperFile.GetFolderSize(RootFolder))) <= HelperParams.GetConfig(PARAM_UPL_JS_SIZE_MAX) Then
hpf.SaveAs(savedFileName)
r.AddLast(New ViewDataUploadFilesResult(FileName, hpf.ContentLength, hpf.ContentType))
Else
r.AddLast(New ViewDataUploadFilesResult(FileName, MSG_ERR_SIZ_TOT))
End If
Else
r.AddLast(New ViewDataUploadFilesResult(FileName, MSG_ERR_FIL_SIZ_MAX))
End If
Else
Using fs = New IO.FileStream(savedFileName, IO.FileMode.Append, System.IO.FileAccess.Write)
Dim PartFromRange As String() = Range(0).Split({" ", "-", "/"}, StringSplitOptions.RemoveEmptyEntries)
Dim Length As Long = CLng(PartFromRange(3))
Dim UploadedSize As Long = CLng(PartFromRange(1))
'Controles de la taille
If fs.Length = UploadedSize Then
'Ecriture
Dim buffer(1024) As Byte
Dim l = hpf.InputStream.Read(buffer, 0, 1024)
While (l > 0)
fs.Write(buffer, 0, l)
l = hpf.InputStream.Read(buffer, 0, 1024)
End While
fs.Flush()
fs.Close()
Dim IsLast As Boolean = (CDbl(PartFromRange(2)) + 1 = Length) '+1 car range commençant à 0
If IsLast Then r.AddLast(New ViewDataUploadFilesResult(FileName, Length, hpf.ContentType))
Else
r.AddLast(New ViewDataUploadFilesResult(FileName, "Perte d'information lors du transfert du fichier."))
End If
End Using
End If
Catch ex As Exception
HelperJournal.WriteException(ex)
r.AddLast(New ViewDataUploadFilesResult(FileName, ex.Message))
End Try
Next
Else
r.AddLast(New ViewDataUploadFilesResult(String.Empty, "Pas de fichier transmis!"))
End If
Dim uploadedFiles = New With {Key .files = r.ToArray}
Dim jsonObj = js.Serialize(uploadedFiles)
context.Response.Write(jsonObj.ToString)
End Sub |
Partager