In case, i'm create function to process excel and in the end of process i want to save excel into my "Download" folder in my pc.. but i get a different folder path between my pc and my server.. when i running my application using my pc, i get path "C:/Users/MyPCUserName/Downloads" but when i running application on published file (iis server), i get path "C:\Users\SYSTEM\Downloads\85FE1000".. I don't know what's the wrong with my code / function..
This my Code :
Private Sub myMethod(ByVal pProjectNo As String, ByVal pOrderNo As String)
Dim dt As DataTable = ClsUploadBreakDownInquiryDB.GetFinalInspectionData(pProjectNo, pOrderNo)
if dt.Rows.Count > 0
'My Function Here
Dim user As String = System.Environment.UserName
exl_b2.SaveAs("C:\Users\" & user & "\Downloads\" & pProjectNo & "_" & Microsoft.VisualBasic.Format (Date.Now, "yyyyMMdd_HHmmss") & ".xlsx", Excel.XlFileFormat.xlOpenXMLWorkbook)
End If
End Sub
CodePudding user response:
You can save a up-loaded file on your web server quite much any place you want.
However, if you talking about a client side user - and their browswer download lcoation? You have ZERO, but 100% ZERO control over that location.
In fact, the user might be using a iPhone, or Android - and not even a desktop comptuer.
You do not have ANY ability in ANY case to control the location of a user download of a file. Files on the local computer are 100% hands off. When they download, it will usually go to their downloads folder - but the user settings control that.
You can't grab, see, or set ANY file location on the client side computer. I mean, if you could do that, then when you come to my site to view a cat picture? My code would then rummage around on your computer - looking for files called passwords, or files called banking etc and steal them
So, server side code behind? Sure, you can in most cases save the file on the web server computer anywhere you like - it don't matter a whole lot.
However, if you talking about client side computer locations for the user and their browser hitting your web site? No, you have zero information, zero control over the users files, and even where they choose to save, or download such files - that is their computer - and browsers give protection for reasons of security.
This can sometimes be confusing when using Visual Studio during development, since your computer, your browser and your web site are all running on the ONE SAME computer, but in a typical deployment, that of course is not the case. So, code behind has zero knowledge about the users local file system.
As a result, you cannot grab, or set ANY kind of file location information on the client side computer.
So, saving to "users" with code behind is ONLY going to apply to the code behind web server "user", and has nothing to do with the client side user.
As a general rule, any and all folders you work with and use from code behind? In EVERY case that should be a sub folder of your root of your project.
Keep in mind:
Any markup code - urls - that is relative to your web site
Any code behind - plane jane windows path name.
So, if you add a folder to your project, it might be say folder UpLoadFiles.
So, then web based, mark up based will be like this:
https://localhost:44392/UpLoadFiles/doc.pdf
So, UpLoadFiles is simple a sub folder in your project.
However, in code behind, your code ALWAYS works with plane jane valid windows file names. So, to convert above to a plane jane file path in windows? You do this:
Dim strFile As String = Server.MapPath("~/UpLoadFiles/abc.txt")
Dim strText As String = File.ReadAllText(strFile)
At this point, now str file is a plane jane valid full good old fashioned windows file name.
So, code behind = always plane jane window path name
So, web markup and URL = always a relative path from your project root.
dim strTextFile = File.ReadAllText(strFile).