/
ivan
/
ModMas
Обзор
Документация
Войти
/
ivan
/
ModMas
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
ModuleMaster/handlers/uploadfile.ashx
108 строк
5 KB
i.krivokon
Initial commit
13 мар 2026, 13:49
13 мар 2026, 13:49
00d9fb6
Код
Авторство
О чём код?
<%@ WebHandler Language="VB" Class="uploadFile" %> Imports System.IO Imports System.Data.SqlClient Imports Newtonsoft.Json Public Class uploadFile : Implements IHttpHandler Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest With context.Request If .InputStream.Length > 0 Then If .Files.Count <> "0" Then Dim tablename As String = getTableName(.Params("tid")) Dim br As New BinaryReader(context.Request.Files(0).InputStream) Dim filename As String = .Files(0).FileName If filename.Contains("/") Then Dim tmp() As String = filename.Split("/") filename = tmp(tmp.Length - 1) End If 'Update file to DB If Not IsNothing(.Params("upd")) Then If .Params("upd").ToUpper = "Y" Then Dim f As String = UpdateFileToDb(tablename, .Params("rownum"), .Params("field"), filename, br.ReadBytes(.Files(0).InputStream.Length)) context.Response.ContentType = "text/json" context.Response.Write("[{""fileid"":""" & f & """},{""url"":""handlers/getfile.ashx?id=" & f & "&storage=[UserModulesAttachments].[user]." & tablename & """}]") Exit Sub End If End If Dim fileid As String = SaveFileToDb(tablename, .Params("rownum"), .Params("field"), filename, br.ReadBytes(.Files(0).InputStream.Length)) context.Response.ContentType = "text/json" context.Response.Write("[{""fileid"":""" & fileid & """},{""url"":""handlers/getfile.ashx?id=" & fileid & "&storage=[UserModulesAttachments].[user]." & tablename & """}]") Else context.Response.Write("No File") End If Else context.Response.Write("No params") End If End With End Sub Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable Get Return False End Get End Property Private Function getTableName(tid As Integer) As String Dim sqlf As New sharedClasses.dbFunctions.SqlFunctions Return sqlf.GetScalar("SELECT dbtablename from usermodules.dbo.moduletables where id = " & tid) End Function Private Function SaveFileToDb(tablename As String, ByVal parentId As Integer, field As String, filename As String, ByVal file() As Byte) As String 'Saving files to DB Dim sqlf As New sharedClasses.dbFunctions.SqlFunctions Dim com As New SqlCommand com.CommandText = "INSERT INTO UserModulesAttachments.[user].[" & tablename & "] " & "([type], [parentId], [name], [doc]) VALUES " & "(@type, @parentid, @name, @doc)" com.Parameters.Clear() With com.Parameters .AddWithValue("type", field) .AddWithValue("parentId", parentId) .AddWithValue("name", filename) .AddWithValue("doc", file) End With Try Dim res As Integer = sqlf.Insert(com) If res > 0 Then sqlf.NonQuery("UPDATE UserModules.[user].[" & tablename & "] SET [" & field & "]='Y' WHERE id=" & parentId) End If Return res Catch ex As Exception Return -1 End Try End Function Private Function UpdateFileToDb(tablename As String, ByVal parentId As Integer, field As String, filename As String, ByVal file() As Byte) As String Dim sqlf As New sharedClasses.dbFunctions.SqlFunctions Dim com As New SqlCommand 'Get top row id of already inserted doc com.CommandText = "SELECT max(id) FROM UserModulesAttachments.[user].[" & tablename & "] WHERE parentid = @parentid and [type] = @type" com.Parameters.Clear() With com.Parameters .AddWithValue("type", field) .AddWithValue("parentId", parentId) End With Dim rowid As String = sqlf.GetScalar(com) If rowid <> "" Then 'File already exist, replacing com.CommandText = "UPDATE UserModulesAttachments.[user].[" & tablename & "] SET [doc] = @doc WHERE id = " & rowid com.Parameters.Clear() With com.Parameters .AddWithValue("doc", file) End With Try sqlf.NonQuery(com) Return rowid Catch ex As Exception Return -1 End Try Else 'No file uploaded yet, inserting Return SaveFileToDb(tablename, parentId, field, filename, file) End If End Function End Class