Hi experts, having this kind of error in my WEB APP. When I'm uploading file through localhost. I can successfully upload. But when access my WEB APP with other local computer. They can't upload with this error.
Here's my function.
Function UploadFile(ByVal uploadCtrl As Object, ByVal oAppType As oAppTypes, ByVal oAppId As String, ByVal LineId As String, Optional ByVal FileDescription As String = "", Optional ByVal IsPublic As oYes_Or_No = oYes_Or_No.N) As Boolean
Try
If Not uploadCtrl.PostedFile Is Nothing And uploadCtrl.PostedFile.FileName <> "" Then
Dim oFileSize As Decimal = uploadCtrl.PostedFile.ContentLength / 1024
Dim oMaxSize As Decimal = GetConfigKey("MaxFileRequest")
If (oMaxSize < oFileSize) Then
SetMessage("You exceeded the allowed maximum size.", oMessageTypes.oError)
Return False
End If
Dim imageSize As Byte() = New Byte(uploadCtrl.PostedFile.ContentLength - 1) {}
Dim uploadedImage__1 As System.Web.HttpPostedFile = uploadCtrl.PostedFile
uploadedImage__1.InputStream.Read(imageSize, 0, CInt(uploadCtrl.PostedFile.ContentLength))
Dim cmd As New SqlCommand()
Dim tableName As String = GetTableName(oAppType)
Dim oFileName As String = uploadCtrl.PostedFile.FileName
Dim fileName As String = oFileName.Substring(oFileName.LastIndexOf("\"), oFileName.Length - oFileName.LastIndexOf("\")).Replace("\", "")
' if lineid is not numeric, being attached is for document
cmd.CommandText = "INSERT INTO [" & FileDbName & "]..FILES (AppType, AppId, LineId, FileDescription, FileName, FileType, FileImage, IsPublic, UploadedBy, DateUploaded, CompanyId) VALUES ('" & oAppType & "', '" & oAppId & "', '" & IIf(IsNumeric(LineId), LineId, -1) & "', '" & TrimData(FileDescription) & "' , '" & TrimData(fileName) & "', '" & Right(oFileName, 3) & "' , @Image, '" & IIf(IsPublic = oYes_Or_No.N, "N", "Y") & "', '" & GetUSER_ID & "', GetDate(), '" & GetCompanyId & "')"
cmd.CommandType = 1
cmd.Connection = oSqlConnection
Dim UploadedImage__2 As New SqlParameter("@Image", SqlDbType.Image, imageSize.Length)
UploadedImage__2.Value = imageSize
cmd.Parameters.Add(UploadedImage__2)
oSqlConnection.Open()
Dim result As Integer = cmd.ExecuteNonQuery()
oSqlConnection.Close()
If result > 0 Then
Return True
End If
Else
SetMessage("Invalid file format.", oMessageTypes.oError)
End If
Catch ex As Exception
SetMessage(ex.Message & "<br> " & strCommand, oMessageTypes.oError)
Return False
End Try
End Function
In my WEB APP
Sub cmdUpload_onClick(Obj AS Object, e As EventArgs) Handles cmdUpload.Click
Try
If FileDescription.Text = "" Then WebCtrl.SetMessage("Please define file description.", 2) : Return
If WebCtrl.UploadFile(fileUploader, GetQString("apptype"), GetQString("appid"), GetQString("lineid"), FileDescription.Text, IIf(IsPublic.Checked, 1, 0)) Then
WebCtrl.SetMessage("Successfully uploaded new file.")
FileDescription.Text = ""
IsPublic.Checked = False
cmdUpload.Enabled = False
End If
Catch Exp As Exception
WebCtrl.SetMessage(Exp)
End Try
End Sub
I already resolve. I notice that when I upload though other machine, the filename already trim.
My work around here is to catch the error.
Dim oFileName As String = uploadCtrl.PostedFile.FileName
Dim fileName As String
Try
fileName = oFileName.Substring(oFileName.LastIndexOf("\"), oFileName.Length - oFileName.LastIndexOf("\")).Replace("\", "")
Catch Exp As Exception
fileName = oFileName
End Try
Marked as answer by hikaru1207 on Jan 21, 2013 08:31 AM
hikaru1207
Member
1 Points
17 Posts
StartIndex cannot be less than zero.
Jan 21, 2013 06:54 AM|LINK
Hi experts, having this kind of error in my WEB APP. When I'm uploading file through localhost. I can successfully upload. But when access my WEB APP with other local computer. They can't upload with this error.
Here's my function.
Function UploadFile(ByVal uploadCtrl As Object, ByVal oAppType As oAppTypes, ByVal oAppId As String, ByVal LineId As String, Optional ByVal FileDescription As String = "", Optional ByVal IsPublic As oYes_Or_No = oYes_Or_No.N) As Boolean Try If Not uploadCtrl.PostedFile Is Nothing And uploadCtrl.PostedFile.FileName <> "" Then Dim oFileSize As Decimal = uploadCtrl.PostedFile.ContentLength / 1024 Dim oMaxSize As Decimal = GetConfigKey("MaxFileRequest") If (oMaxSize < oFileSize) Then SetMessage("You exceeded the allowed maximum size.", oMessageTypes.oError) Return False End If Dim imageSize As Byte() = New Byte(uploadCtrl.PostedFile.ContentLength - 1) {} Dim uploadedImage__1 As System.Web.HttpPostedFile = uploadCtrl.PostedFile uploadedImage__1.InputStream.Read(imageSize, 0, CInt(uploadCtrl.PostedFile.ContentLength)) Dim cmd As New SqlCommand() Dim tableName As String = GetTableName(oAppType) Dim oFileName As String = uploadCtrl.PostedFile.FileName Dim fileName As String = oFileName.Substring(oFileName.LastIndexOf("\"), oFileName.Length - oFileName.LastIndexOf("\")).Replace("\", "") ' if lineid is not numeric, being attached is for document cmd.CommandText = "INSERT INTO [" & FileDbName & "]..FILES (AppType, AppId, LineId, FileDescription, FileName, FileType, FileImage, IsPublic, UploadedBy, DateUploaded, CompanyId) VALUES ('" & oAppType & "', '" & oAppId & "', '" & IIf(IsNumeric(LineId), LineId, -1) & "', '" & TrimData(FileDescription) & "' , '" & TrimData(fileName) & "', '" & Right(oFileName, 3) & "' , @Image, '" & IIf(IsPublic = oYes_Or_No.N, "N", "Y") & "', '" & GetUSER_ID & "', GetDate(), '" & GetCompanyId & "')" cmd.CommandType = 1 cmd.Connection = oSqlConnection Dim UploadedImage__2 As New SqlParameter("@Image", SqlDbType.Image, imageSize.Length) UploadedImage__2.Value = imageSize cmd.Parameters.Add(UploadedImage__2) oSqlConnection.Open() Dim result As Integer = cmd.ExecuteNonQuery() oSqlConnection.Close() If result > 0 Then Return True End If Else SetMessage("Invalid file format.", oMessageTypes.oError) End If Catch ex As Exception SetMessage(ex.Message & "<br> " & strCommand, oMessageTypes.oError) Return False End Try End FunctionIn my WEB APP
Sub cmdUpload_onClick(Obj AS Object, e As EventArgs) Handles cmdUpload.Click Try If FileDescription.Text = "" Then WebCtrl.SetMessage("Please define file description.", 2) : Return If WebCtrl.UploadFile(fileUploader, GetQString("apptype"), GetQString("appid"), GetQString("lineid"), FileDescription.Text, IIf(IsPublic.Checked, 1, 0)) Then WebCtrl.SetMessage("Successfully uploaded new file.") FileDescription.Text = "" IsPublic.Checked = False cmdUpload.Enabled = False End If Catch Exp As Exception WebCtrl.SetMessage(Exp) End Try End SubPlease help.
Regards,
hikaru1207
Member
1 Points
17 Posts
Re: StartIndex cannot be less than zero.
Jan 21, 2013 08:31 AM|LINK
I already resolve. I notice that when I upload though other machine, the filename already trim.
My work around here is to catch the error.
Dim oFileName As String = uploadCtrl.PostedFile.FileName Dim fileName As String Try fileName = oFileName.Substring(oFileName.LastIndexOf("\"), oFileName.Length - oFileName.LastIndexOf("\")).Replace("\", "") Catch Exp As Exception fileName = oFileName End TryAmy Peng - M...
Star
10199 Points
964 Posts
Microsoft
Re: StartIndex cannot be less than zero.
Jan 22, 2013 05:54 AM|LINK
Hi,
I am glad that you have solved your problem by yourself.
Welcome to post your question in asp.net Forums next time.
Best Regards,
Amy Peng
Feedback to us
Develop and promote your apps in Windows Store