Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Fileupload save to folder extension problems

Status
Not open for further replies.

sirnose1

Programmer
Nov 1, 2005
133
US
I have some code that uploads a document, renames the file based on the the concat of the year and a users id. (ex. "2014-9806543210"). It should overwrite the file no matter what the extension, so it could be stored as "2014-9806543210.xls" "2014-9806543210.xlsx", "2014-9806543210.docx", etc. As long as the extension is the same, it will overwrite. I have been trying to use the wildcard ("*") to no avail. The following code does that:

Protected Sub OnConfirm(sender As Object, e As EventArgs)
Dim supplierCID = Session("CID")

fileName = fuProductCapsuleEXCEL.FileName.ToString
Dim newFileName As String = ""
Dim saveInfo As String
Dim ext As String = Path.GetExtension(fuProductCapsuleEXCEL.FileName).Substring(1)

salesYear = Request.QueryString("salesyear")

'If ext.Trim().ToLower() = "xls" Or ext.Trim().ToLower() = "xlsx" Then
saveInfo = "Original File Name: " & fileName & "<br/>"
saveInfo += "File Size: " & upload1.PostedFile.ContentLength / 1000 & " Kilobytes <br/>"
saveInfo += "Content Type: " & upload1.PostedFile.ContentType & "<br/>"
newFileName = salesYear + "-" + supplierCID + "." + ext
saveInfo += "New File Name: " + newFileName.ToString()
lblSaveInfo.Text = saveInfo
'check if file already exist
exist = CheckForDup(newFileName)
Dim confirmValue As String = Request.Form("confirm_value")

If exist.ToString() = True Then
If confirmValue = "Yes" Then
'save file
upload1.SaveAs(Application("IMARK_RootPath") & "/UPLOADED_FILE/G2G_SKUs/" + newFileName)
ClientScript.RegisterStartupScript(Me.[GetType](), "alert", "alert('File saved as " & newFileName & "')", True)
Else
'dont save file
ClientScript.RegisterStartupScript(Me.[GetType](), "alert", "alert('File saved as " & newFileName & "')", True)
End If
End If

If exist = False Then
upload1.SaveAs(Application("IMARK_RootPath") & "/UPLOADED_FILE/G2G_SKUs/" + newFileName)
ClientScript.RegisterStartupScript(Me.[GetType](), "alert", "alert('File saved as " & newFileName & "')", True)
End If
End Sub


Protected Function CheckForDup(ByVal newFileName As String) As Boolean

Dim supFldr As String = Application("IMARK_RootPath") & "/UPLOADED_FILE/G2G_SKUs/"
Dim savePath As String = Path.Combine(supFldr, newFileName)
exist = False

If File.Exists(savePath) Then
exist = True
Else
exist = False
End If

Return exist
Response.Write(exist)
Response.End()
End Function

I have code in another page that displays a hyperlink to the file if there is a file in the folder. It is set to just read .xlsx files because I don't know how to use other extensions for this problem. Your assistance would be greatly appreciated.

Thanks n advance
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top