| |

VerySource

 Forgot password?
 Register
Search
View: 977|Reply: 9

How ASP.NET determines if a file exists on the hard drive

[Copy link]

1

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 Invalid IP Address

Post time: 2020-2-2 13:00:02
| Show all posts |Read mode
Using the built-in FileUpload control, I want to perform error handling without this file.
My code looks like this:
HttpFileCollection uploadedFiles = Request.Files;
for (int i = 0; i <uploadedFiles.Count; i ++)
{
    HttpPostedFile userPostedFile = uploadedFiles [i];
    if (File.Exists (userPostedFile.FileName))
    {
        Upload
    }
    else
    {
        Error
    }
}
The problem is that the absolute path of the file can be obtained at userPostedFile.FileName, such as: "C:\\Documents and Settings\\Admin\\Desktop\\aaa.txt", but whether the file exists or not, the result is false ..... . I don't know what's wrong with this method, or there are other better ways to determine if the file exists? Ask the master!
Reply

Use magic Report

0

Threads

23

Posts

21.00

Credits

Newbie

Rank: 1

Credits
21.00

 China

Post time: 2020-3-20 12:30:01
| Show all posts
Show yourself the code
// Get the file name in the control
string fileName = this.txtFile.PostedFile.FileName.Substring (this.txtFile.PostedFile.FileName.LastIndexOf ("\\"));
// Set server path folder and file
string sFileName = Server.MapPath (Request.ApplicationPath) + "\\fileFavorite" + fileName;

// Get the type of file
string fileJudge = fileName.Substring (fileName.LastIndexOf (".") + 1);
if (fileJudge.Equals ("pdf") || fileJudge.Equals ("PDF") || fileJudge.Equals ("xls") || fileJudge.Equals ("XLS") ||
fileJudge.Equals ("doc") || fileJudge.Equals ("DOC") || fileJudge.Equals ("txt") || fileJudge.Equals ("TXT") ||
fileJudge.Equals ("ppt") || fileJudge.Equals ("PPT") || fileJudge.Equals ("csv") || fileJudge.Equals ("CSV"))
{
if (System.IO.File.Exists (Server.MapPath (Request.ApplicationPath) + "\\fileFavorite" + fileName) == false)
{
try
{
string newFileName = this.newFileNameUrl (); // new file name uploaded to the server
Ranch
this.txtFile.PostedFile.SaveAs (Server.MapPath (Request.ApplicationPath) + "\\fileFavorite" + "\\" + newFileName);
return true;
}
catch (Exception ex)
{
ex.GetType ();
Response.Write ("<script> onload = function () {alert ('Upload failed for network reasons!');} </ Script>");
return false;
}
}
else
{
Response.Write ("<script> onload = function () {alert ('File already exists!');} </ Script>");
return false;
}
}
else
{
Response.Write ("<script> onload = function () {alert ('The format of the file to be uploaded is incorrect!');} </ Script>");
return false;
}
Reply

Use magic Report

0

Threads

73

Posts

27.00

Credits

Newbie

Rank: 1

Credits
27.00

 China

Post time: 2020-3-20 13:30:01
| Show all posts
userPostedFile.FileName is just a file name without a path
Reply

Use magic Report

0

Threads

8

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

Post time: 2020-3-20 16:30:01
| Show all posts
The file name in the upload control under asp.net is a client-side file, not a server-side file. C # is running on the server side. You cannot judge whether the file exists based on this.
Reply

Use magic Report

0

Threads

322

Posts

115.00

Credits

Newbie

Rank: 1

Credits
115.00

 China

Post time: 2020-3-20 17:15:01
| Show all posts
You want to determine whether the file to be saved exists, not whether the original file exists

string newPath = Server.Mappath (".. *****");

if (! File.Exists (newPath))
{
    //save.
}
Reply

Use magic Report

0

Threads

73

Posts

27.00

Credits

Newbie

Rank: 1

Credits
27.00

 China

Post time: 2020-3-20 19:00:01
| Show all posts
userPostedFile.FileName is just a file name on the client, without the path
such as:
if (File.Exists ("c:\\" + userPostedFile.FileName)) // here c:\\is replaced with the directory you want to upload to
    {
        Upload
    }
    else
    {
        Error
    }
Reply

Use magic Report

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-3-20 23:00:01
| Show all posts
if (File.PostedFile.ContentLength! = 0)
{
 File.PostedFile.SaveAs ("path");
}
Reply

Use magic Report

1

Threads

12

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

Post time: 2020-3-23 07:45:02
| Show all posts
You cannot access the client's hard drive


Consider this security aspect.
Reply

Use magic Report

0

Threads

38

Posts

22.00

Credits

Newbie

Rank: 1

Credits
22.00

 China

Post time: 2020-3-23 19:45:01
| Show all posts
Judgment. ContentLength == 0
    HttpPostedFile userPostedFile = uploadedFiles [i];
    if (userPostedFile.ContentLength! = 0)
    {
        save
    }
    else
    {
        Error
    }
Reply

Use magic Report

0

Threads

73

Posts

27.00

Credits

Newbie

Rank: 1

Credits
27.00

 China

Post time: 2020-3-23 21:45:01
| Show all posts
Oh, the landlord wants to determine whether the client's file exists? Then use the method upstairs
if (userPostedFile.ContentLength! = 0)
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list