通過Web Services上載檔案

2021-04-12 13:38:13 字數 2720 閱讀 8914

向伺服器上載檔案可能有許多種方法,在利用web services上載檔案的方法中,下面的這個方法應該是最簡單的了。首先建立upload.a**x檔案,其upload.a**x.cs內容如下,裡面已經做了注釋:

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

#region component designer generated code

//web 服務設計器所必需的

private icontainer components = null;

///

/// 設計器支援所需的方法 - 不要使用**編輯器修改

/// 此方法的內容。

///

private void initializecomponent()

///

/// 清理所有正在使用的資源。

///

protected override void dispose( bool disposing )

base.dispose(disposing);

}#endregion

[webmethod(description="web 服務提供的方法,返回是否檔案上載成功與否。")]

public string uploadfile(byte fs,string filename)

catch(exception ex)}}

}要上載檔案,必須提供乙個表單,來供使用者進行檔案的選擇,下面我們就建立這樣乙個頁面upload.aspx,用來提供檔案上載:

<%@ page language="c#" codebehind="upload.aspx.cs" autoeventwireup="false"

inherits="aspxwebcs.upload" %>

我們要進行處理的是在後**裡面,下面詳細的介紹,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

#region web form designer generated code

override protected void oninit(eventargs e)

///

/// 設計器支援所需的方法 - 不要使用**編輯器修改

/// 此方法的內容。

///

private void initializecomponent()

#endregion

private void button1_click(object sender, system.eventargs e)

string filepath = ofiles[0].filename;

if(filepath == "" || filepath == null)

string filename = filepath.substring(filepath.lastindexof("//")+1);

trycatch(exception ex)

}else}}

}最後,需要注意的是:在儲存檔案時,您應該確保指定檔案的完整路徑(例如,"c:/myfiles/picture.jpg"),並確保為 asp.net 使用的帳戶提供要儲存檔案的目錄的寫許可權。上載大檔案時,可使用 元素的 maxrequestlength 屬性來增加檔案大小的最大允許值,例如:

其中:maxrequestlength:指示 asp.net 支援的http方式上載的最大位元組數。該限制可用於防止因使用者將大量檔案傳遞到該伺服器而導致的拒絕服務攻擊。指定的大小以 kb 為單位。預設值為 4096 kb (4 mb)。executiontimeout:指示在被 asp.net 自動關閉前,允許執行請求的最大秒數。在當檔案超出指定的大小時,如果瀏覽器中會產生 dns 錯誤或者出現服務不可得到的情況,也請修改以上的配置,把配置數加大。

另外,上載大檔案時,還可能會收到以下錯誤資訊:

aspnet_wp.exe (pid: 1520) 被**,因為記憶體消耗超過了 460 mb(可用 ram 的百分之 60)。

如果遇到此錯誤資訊,請增加應用程式的 web.config 檔案的 元素中 memorylimit 屬性的值。例如:

我在自己的機器上測試,可以上傳50m以上的檔案。以上**在windows xp + .net 1.0 + vs.net2002下測試通過。 

delphi中通過TIdhttp控制項上載檔案

附帶的例子由於indy版本的問題,有點小問題 其實用文字工具修改一下dfm,去掉新增的屬性就可以了 我乾脆把原始碼給貼出來吧,版權屬於原作者 一.用於實現form流格式的單元 m ultipartformdata.pas unit m ultipartformdata inte ce uses sy...

通過反射動態呼叫webservices

該方法可以使程式不通過web引用的方式去呼叫webservices方法,直接在 裡呼叫該方法就能達到動態呼叫webservices的目的。使用前先引用system.web.services動態鏈結庫,是.net自帶的dll。方法如下 using system using system.collect...

通過工具向Oracle中上載資料

一 使用shp2sdo並結合sql loader匯入 shp檔案 shp2sdo是oracle提供的乙個工具,幫助使用者匯入 shp檔案到oracle spatial中。準備材料 shp2sdo.exe可執行檔案 空間資料 此處以arcgis自帶的world資料為例 在windows作業系統下的匯入...