Big file download problem (solve security, speed, breakpoint resume, multi-thread download, server high occupancy issues)

xiaoxiao2021-03-06  14

Namespace

Downfile

{Using system.web; using system.net; using system.text; using system.data; using system.data.sqlclient; / ** // * * 1. Support breakpoint resume * 2. Support multi-thread download * 3. Solution speed problem * 4. File play resolution Drag problem * 5. Solve the server resources High occupancy issues * / //////

/// DownFile Summary Description .

/// public class getfile: system.Web.ui.page {/ ** // * * Support breakpoint resumed * Solution speed problem * Support big file * / private string file; private string filehead; private; string sqlConnString; public string sqlConnString {get {if (sqlConnString == null) sqlConnString = System.Configuration.ConfigurationSettings.AppSettings [ "sqlConnString"]; return sqlConnString;}} private string headFilePath; public string headFilePath {get {if (headFilePath = = null) headFilePath = System.Configuration.ConfigurationSettings.AppSettings [ "HeadFilePath"]; return headFilePath;}} private void Login () {// capture the UserName and PassWord from IChannal; // string loginUrl = System.Configuration.ConfigurationSettings.AppSettings [" LoginUrl "]; //" asdfffgsdf - = - sd09-593% @ # 42593049523-4 "// loginUrl = "Name=111@1111.com"; "userinfo"; if (userinfo == null); if (userinfo == null) {// User does not log in // Response.write ! "); Response.statusdesdescribTION =" User did not log in! "; Responseend (407);} loginurl =

userInfo; // WebClient achieved using remote login verification results obtained WebClient wc = new WebClient (); wc.Credentials = CredentialCache.DefaultCredentials; try {byte [] loginData = wc.DownloadData (loginUrl); string loginResult = Encoding.Default. GetString (logindata); // 1 Landing success 0 Landing failure 2 No user name IF (system.convert.toint32 (loginResult) == 1) Return; else if (system.convert.toint32 (loginResult) == 0) { // User login check failed //Response.write ("User login check failed!"); Response.statusdescription = "Landing failed!"; Responsend (418);} else {// (System.convert.Toint32 ( LoginResult) == 2) {//response.write ("There is no change user name!"); response.statusdescription = "Landing failed!"; responseEnd (419);}} catch (exception) {// You cannot access the URL login, the login result is abnormal, and the login failed. Response.statusdescribTION = "Cannot access the URL login, the login result is exception, the land failed!"; ResponseEnd (420);}} private void permit ()}} private void permit ()} "cguid"]; string filename = Request.QueryString ["f"]; if (cguid == null || filename == null) {response.statusdescription = "parameter error."; ResponseEnd (412);} SQLConnection Sqlconn = New SqlConnection (SqlConnString); sqlconn.open (); // Check Download String SQL;

SqlCommand sqlCmd = new SqlCommand (); sqlCmd.CommandType = CommandType.Text; sqlCmd.Connection = sqlConn; sql = @ "Select Retailers.RetailerUid From Retailers Inner Join LimitDownLoad on LimitDownLoad.RetailerId = Retailers.Id Where LimitDownLoad.RetailerId = (Select RetailerId From LimitDownLoad Where ContentGuid = @ ContentGuid) Group By Retailers.LimitDown, Retailers.RetailerUid Having Sum (LimitDownLoad.DownCount)

Sqlcmd.parameters.add ("@ filename", sqldbtype.varchar, 100); sqlcmd.parameters ["@ filename"]. value = filename; object objfile = sqlcmd.executescalar (); if (Objfile == null) {/ The / database does not have a package file, ie the file is not packaged response.statusdescription = "There is no package file in the database."; SqlConn.close (); responseEnd (404);} // Over the database Absolute physical path file = objfile.toString (); // Handling file = file.replace (@ @@ "///192.168.2.3/001.WMV is equivalent. / ", @" / "); File = @" / " file; // -------- End filehead = headfilepath rguid @" / " getHeadFile (file); filehead = filehead.replace @ "//", @ "/"); // filehead = @ "/" filehead; if (! File.exists (file) ||! File.exists (filehead)) {// file or header file is not The existence! Response.statusdescription = "file or header file does not exist!"; // or no access to the file SQLConn.close (); Responseend (404);} // Update Downloads / ** // * * This part iChannal is downloaded after downloading SQL = "Update LimitdownLoad Set DownCount = DOWNCOUV 1 where contentguid = @ ContentGUID"; sqlcmd.commandtext = SQL Sqlcmd.parameters.clear (); sqlcmd.parameters.add ("@ contentguid", sqldbtype.varchar, 36); sqlcmd.Parameters ["@ ContentGuid"]. Value = cguid; sqlcmd.executenonquery ();

* / SqlConn.Close ();} private void ResponseEnd (int statusCode) {Response.StatusCode = statusCode; Response.End ();} private string GetHeadFile (string file) {string f1 = Path.GetFileNameWithoutExtension (file); string f2 = Path.Getextension (file); Return F1 "_ h" f2;} private string getmime (string fileEname) {string fileExtent = path.Getextension (filename); // int index = filename.lastIndexof ('); / /filename.substring (index ,filename.length; string retmi; switch (fileext) {copy ".rmime =" Application / VND.RN-RealMedia "; Break; Case" .mp3 ": Retmime = "Application / MP3"; Break; Case ".asf": Retmime = "VIDEO / X-MS-ASF"; BREAK Case ".avi": retmime = "video / avi"; break; case ".wav": retmime = "Audio / WAV"; break; case ".wmv": retmime = "Audio / WMV"; Break; Case .mpg ": retmime =" video / mpeg "; break; case" .mpeg ": retmime =" video / mpeg ";

Default: retmime = "Application / OCTET-Stream"; Break;} Return Retmime;} protected override void oninit (Eventargs E) {// login check Login (); // Filter limit condition permit (); // file = @ "C: / xx.wmv"; // filehead = @ "c: /xx_h.wmv"; // File to open filestream fs = new filestream (file, filemode.open, fileaccess.read, Fileshare.Read) ); Response.Clear (); Response.ContentType = getMime (file); // Range: bytes = 449794- || bytes = 449794-588888 FileStream fsHead = new FileStream (fileHead, FileMode.Open, FileAccess.Read, FileShare. Read); BYTE [] BUFF = New Byte [209152]; // File length, each different OFFSET request, throwing the file size of the client as a change line length = fs.length-10240 fsHead.Length; / / Get the client request file positioning position String Range = request.Headers ["RANGE"]; / / The default starting position is the beginning of the file, the end position is the end of the file // Remove the head 10k size of the file! Long offset = 0, endset = fs.length-10240 fshead.length; int LEN; // actually read the byte of the number of bytes of the number of files response.Appendheader ("Content-Disposition", " Attachment; filename = " path.getFileName (file)); response.buffer = true; response.statuscode = 200; if (range! = null && range! =") {response.statuscode = 206; int index = range .Indexof ('='); Range = Range.Substring (INDEX 1, Range.Length-Index-1); index =

转载请注明原文地址:https://www.9cbs.com/read-49925.html

New Post(0)