Upload and download files through Web Services (2)

xiaoxiao2021-03-06  70

Second, upload the file via Web SERVICES

There are many ways to upload files to the server. In the method of loading files with Web Services, the following methods should be the easiest. We are still like the previous example, first build upload.asmx files, which is as follows, which has been done in the following:

Using system;

Using system.collections;

Using system.componentmodel;

Using system.data;

Using system.diagnostics;

Using system.Web;

Using system.Web.services;

Using system.io;

Namespace Xml.sz.luohuedu.net.aspxwebcs

{

///

/// UPLOAD summary description. ///

[WebService (Namespace = "http://xml.sz.luohuedu.net/",

Description = "Use the .NET framework to upload files in Web Services.")]

Public class upload: System.Web.Services.WebService

{

Public Upload ()

{

// Codegen: This call is necessary for the ASP.NET Web service designer.

InitializationComponent ();

}

#Region Component Designer Generated Code

/ / Web service designer necessary

Private icontainer Components = NULL;

///

/// Designer supports the required method - Do not use the code editor to modify the // / this method. ///

Private vidinitiRizeComponent ()

{

}

///

/// Clean all the resources being used. ///

Protected Override Void Dispose (Bool Disposing)

{

IF (Disposing && Components! = NULL)

{

Components.dispose ();

}

Base.dispose (Disposing);

}

#ndregion

[WebMethod (Description = "WEB service provides the way to return to whether the file is successfully or not.")]

Public string UploadFile (byte [] fs, string filename)

{

Try

{

/// Define and instantiate a memory stream to store an array of bytes submitted.

MemoryStream M = New MemoryStream (fs);

// / Define the actual file object and save the uploaded file.

FILESTREAM F = New FileStream (Server.mAppath (".") "//"

FileName, FileMode.create;

/// Write the data in the inner memory into physical files

M.Writto (f);

m.Close ();

f.close ();

f = NULL;

m = NULL;

The return "file has been uploaded.";

}

Catch (Exception EX)

{

Return ex.Message;

}

}

}

}

To upload files, you must provide a form to provide the user's selection. Let's establish such a page UPLoad.aspx to provide file upload:

<% @ Page language = "c #" codebehind = "UPLOAD.ASPX.CS" autoeventwireup = "false" inherits = "aspxwebcs.upload"%>

What we have to process is in the post-sample code, the following detailed introduction, Upload.aspx.cs:

Using system;

Using system.collections;

Using system.componentmodel;

Using system.data;

Using system.drawing;

Using system.Web;

Using system.Web.SessionState;

Using system.Web.ui;

Using system.Web.ui.webcontrols;

Using system.Web.ui.htmlcontrols;

Using system.Web.services;

Using system.io;

Namespace aspxwebcs

{

///

/// UPLOAD summary description. /// Using this method overload files over the web service ////

Public class upload: system.Web.ui.page

{

Protected system.Web.ui.htmlcontrols.htmlinputfile myfile;

protected system.web.ui.webcontrols.button button1;

Private Void Page_Load (Object Sender, System.EventArgs E)

{

/ / Place the user code here to initialize the page

}

#Region Web Form Designer Generated Code

Override protected void oninit (Eventargs E)

{

//

// Codegen: This call is necessary for the ASP.NET Web Form Designer.

//

InitializationComponent ();

Base.onit (E);

}

///

/// Designer supports the required method - Do not use the code editor to modify the // / this method. ///

Private vidinitiRizeComponent ()

{

This.Button1.click = new system.eventhandler (this.button1_click);

This.Load = New System.EventHandler (this.page_load);

}

#ndregion

Private void Button1_Click (Object Sender, System.Eventargs E)

{

/// First get upload file information and file flow

IF (myfile.postedfile! = null)

{

System.Web.httpFileCollection ofiles;

Ofiles = system.web.httpContext.current.request.files;

IF (OFLILES.COUNT <1)

{

Response.write ("Please select a file.");

Response.end ();

}

String filepath = ofiles [0] .filename;

IF (filepath == "|| filepath == NULL)

{

Response.write ("Please select a file.");

Response.end ();

String filename = filepath.substring (filepath.lastIndexof ("//") 1);

Try

{

/// Hand upload information.

Byte [] b = new byte [ofiles [0] .contentLENGTH];

System.io.stream fs;

Xml.sz.luohuedu.net.aspxwebcs.upload O;

o = new xml.sz.luohuedu.net.aspxwebcs.upload ();

FS = (System.io.Stream) ofiles [0] .inputstream;

fs.read (b, 0, ofiles [0] .contentLength);

/// Call the Web Services' UPLoadFile method to upload files.

Response.write (O. UploadFile (B, FileName));

fs.close ();

}

Catch (Exception EX)

{

Response.write (ex.Message);

}

}

Else

{

Response.write ("Please select File");

}

}

}

}

Finally, you need to pay attention to: When saving files, you should make sure that the full path to the specified file (for example, "c: /myfiles/picture.jpg), and make sure the account to be stored for the ASP.NET provides the directory to store the file. Write permissions. Upload big files

The maxrequestlength attribute of the element increases the maximum allowable value of the file size, for example:

Where: maxRequestLength: Indicates the maximum number of bytes uploaded by the HTTP mode supported by ASP.NET. This limitation can be used to prevent denial of service attacks caused by users from passing a large number of files to the server. The specified size is in KB. The default is 4096 KB (4 MB). ExecutionTIMEOUT: The maximum number of seconds allowing the request is allowed before being automatically turned off by ASP.NET. In the case where the file is exceeded, if the DNS error is generated or the service is not available, please modify the above configuration, increase the configuration number.

In addition, when uploading large files, you may also receive the following error message:

ASPNET_WP.EXE (PID: 1520) is reclaimed because the memory consumption exceeds 460 MB (can be used by 60% of RAM).

If you encounter this error message, add the application's Web.config file.

The value of the MEMORYLIMIT property in the element. E.g:

I test on my own machine, you can upload 50m or more files. The above code is passed under Windows XP .NET 1.0 vs.net2002. .

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

New Post(0)