Read, upload, and download files

Source: Internet
Author: User

// Read File Information
String filecate = system. configuration. configurationmanager. receivettings ["filecatelog"];
String fielpath = server. mappath ("") + "\" + filecate + "\\";
String [] fname = directory. getfiles (fielpath );
For (INT I = 0; I <fname. length; I ++)
{
String fn = "";
Long Len = 0;
String creatdate = "";
Fileinfo fi1 = new fileinfo (fname [I]. tostring ());
If (fi1.exists)
{
Creatdate = fi1.creationtime. tostring ("mm DD, YYYY ");
Len = fi1.length;
Filetype = fi1.extension. tostring ();
}

Fn = fname [I]. substring (fname [I]. lastindexof ("\") + 1, fname [I]. length-fname [I]. lastindexof ("\")-1 );

}
// Save the uploaded file
Private void uploadsaveas ()
{
Int filecount = request. Files. count;
If (filecount> 0)
{
String [] afilename;
String uploadpath;

Uploadpath = system. configuration. configurationmanager. receivettings ["filecatelog"] + "\\";
Char [] De = {'\\'};
Httppostedfile HPF = request. Files [0];
Afilename = HPF. filename. Split (de );
String uploadfilename = afilename [afilename. Length-1];

If (uploadfilename! = "" & HPF. contentlength> 0)
{
If (HPF. contentlength> 20000*1024)
{
Response. Write ("<script language = 'javascript '> alert ('file is too large to upload! '); </SCRIPT> ");
Return;
}
String filecate = system. configuration. configurationmanager. receivettings ["filecatelog"];
String fielpath = server. mappath ("") + "\" + filecate + "\\";
String [] fname = directory. getfiles (fielpath );
For (INT I = 0; I <fname. length; I ++)
{
Fileinfo fi1 = new fileinfo (fname [I]. tostring ());
If (fi1.name = uploadfilename)
{
Response. write ("<script language = 'javascript '> alert ('this file already exists on the server. Please select another file or change the file name and upload it again'); </SCRIPT> ");

// Response. Write ("<SCRIPT> alert ('the file with the same name already exists on the server. Please select another file or change the file name and upload it again ') </SCRIPT> ");

Return;

}
}
String strfilepath = This. Request. filepath;
String strpath = path. getdirectoryname (strfilepath );
String destfilename = uploadpath + uploadfilename;
Try
{
HPF. saveas (server. mappath (destfilename ));
This. lbmsg. Text = "the file is uploaded successfully! ";

}
Catch (exception ex)
{
This. lbmsg. Text = ex. tostring ();
}
}
Else if (HPF. contentlength> = 0)
{
Response. Write ("<script language = 'javascript '> alert (' the file content is empty. Please select another file! '); </SCRIPT> ");
Return;
}
}
}
// Download an object
1. Bind the connection to the file name
(Hyperlink) This. gridview1.rows [I]. cells [3]. findcontrol ("hyperlink1 ")). navigateurl = filecate + "/" + this. gridview1.rows [I]. cells [0]. text;
2. Directly write in table
TC. TEXT = "<a Title = \" click to download \ "href = \" "+ file directory +" \ "+ file name +" \ ">" + file name + "</ a> ";

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.