HTML plus C # uploading files

Source: Internet
Author: User

Recently learning to upload files part of the content, including the creation of folders, set folder properties, upload files and save.

Front Code:

<HTML xmlns="http://www.w3.org/1999/xhtml">

<head runat="Server">

<meta http-equiv= "content-type" content= "text/html; Charset=utf-8 " />

< title></title>

</head> <body>

  <form runat= "server"  id= "Form1"  method= "post"  enctype= "Multipart/form-data" >

&NBSP; < Input name= "F"  type=< Span class= "Attribute-value" > "file"  />  

<input name="s" type="Submit" />

</form>

</body>

</html>

Background code:

System.Web.HttpFileCollection _file = System.Web.HttpContext.Current.Request.Files;

if (_file. Count > 0)

{

File size

Long size = _file[0]. ContentLength;

File type

String type = _file[0]. ContentType;

Filename

String name = _file[0]. FileName;

File format

String _tp = System.IO.Path.GetExtension (name);

if (_TP. ToLower () = = ". jpg" | | _tp. ToLower () = = ". jpeg" | | _tp. ToLower () = = ". gif" | | _tp. ToLower () = = ". png" | | _tp. ToLower () = = ". swf")

{

Get file stream

System.IO.Stream Stream = _file[0]. InputStream;

Save File

String savename = DateTime.Now.ToString ("YYYYMMDDHHMMSS") + _tp;

String path = Server.MapPath ("") + "/upload/area/" + savename;

_file[0]. SaveAs (path);

}

}

Later thought, how to determine whether the folder exists? If it does not exist, the direct save will be wrong, or already exist, will not overwrite it?

Use the following method to determine if there is a non-existent create

if (! Directory.Exists (spath))
{
Directory.CreateDirectory (spath);

}

The folder was later created, or the path error was reported, the reason conjecture: 1, not included in the project. 2, no permissions.

Workaround:

1, it is possible to manually include it in the project, it can be solved, but very uncomfortable, because the program runs on the server, you can not create a folder, and then manually to add him into the project.

2, the online search is created by the folder is read-only properties, but I manually put the program created by the folder properties read-only removed, or unsuccessful, but use the following code to remove the folder Properties "read-only", the upload succeeded.

Remove read-only properties for folder: System.IO.DirectoryInfo dirinfo = new DirectoryInfo ("filepath");
Dirinfo.attributes = Fileattributes.normal & fileattributes.directory;

Remove read-only properties of the file: System.IO.File.SetAttributes ("filepath", System.IO.FileAttributes.Normal);

After that, the new folder part of the code was re-organized into:

String _tp = System.IO.Path.GetExtension (name);

if (_TP. ToLower () = = ". jpg" | | _tp. ToLower () = = ". jpeg" | | _tp. ToLower () = = ". gif" | | _tp. ToLower () = = ". png" | | _tp. ToLower () = = ". swf")
{

String savename = DateTime.Now.ToString ("YYYYMMDDHHMMSS") + _tp;
string file = "/" + DateTime.Now.ToString ("yyyyMMdd") + "/";
String path = Server.MapPath ("~") +file;

if (! Directory.Exists (PATH))
{
Directory.CreateDirectory (path);
DirectoryInfo dirinfo = new DirectoryInfo (path);
Dirinfo.attributes = fileattributes.normal&fileattributes.directory;
}
_file[0]. SaveAs (Path+savename);
Viewbag.imgpath = file + savename;//can be saved to a database or XML and other places, I am here to test, directly back to the front desk

}

HTML plus C # uploading files

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.