0
我需要通過WebService的從一個asp.net網頁表單應用程序上傳文件(的.asmx),我轉換爲字節,並保存到數據庫中。上傳文件
但是問題是文件總是上傳兩次,這會在我的數據庫表中創建一個副本。當我啓動調試模式時,中斷點向上移動,就好像我點擊了兩次開始上傳按鈕。
我使用這個庫Simple example of ajax file upload,我將不勝感激,如果你有答案了這一技術難題。我搜索了很多小時,但是我還沒有找到任何東西。
編輯 要回答駐軍尼利:代碼當頁面加載
protected override void OnInit(EventArgs e)
{
this.ScriptManager1.AsyncPostBackTimeout = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["AsyncPostBackTimeout"]);
this.ParentRepeaterBind();
base.OnInit(e);
}
protected void Page_Load(object sender, EventArgs e)
{
}
而且在上傳按鈕的代碼是:
/// <summary>
/// Summary description for WebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService
{
[WebMethod, ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string HelloWorld()
{
byte[] bytes;
if (Context.Request.Files[0] != null && Context.Request.Files[0].InputStream != null)
{
bytes = ReadStream(Context.Request.Files[0].InputStream);
// Read the file and convert it to Byte Array
string filePath = Context.Request.Files[0].FileName;
string filename = Path.GetFileName(filePath);
string ext = Path.GetExtension(filename);
string contenttype = String.Empty;
//Set the contenttype based on File Extension
switch (ext)
{
case ".doc":
contenttype = "application/vnd.ms-word";
break;
case ".docx":
contenttype = "application/vnd.ms-word";
break;
case ".xls":
contenttype = "application/vnd.ms-excel";
break;
case ".xlsx":
contenttype = "application/vnd.ms-excel";
break;
case ".jpg":
contenttype = "image/jpg";
break;
case ".png":
contenttype = "image/png";
break;
case ".gif":
contenttype = "image/gif";
break;
case ".pdf":
contenttype = "application/pdf";
break;
}
if (contenttype != String.Empty)
{
Stream fs = Context.Request.Files[0].InputStream;
BinaryReader br = new BinaryReader(fs);
//insert the file into database
string strQuery = "insert into dbo.FileUpload(Name, ContentType, Data) values (@Name, @ContentType, @Data)";
SqlCommand cmd = new SqlCommand(strQuery);
cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = filename;
cmd.Parameters.Add("@ContentType", SqlDbType.VarChar).Value = contenttype;
cmd.Parameters.Add("@Data", SqlDbType.Binary).Value = bytes;
InsertUpdateData(cmd);
}
else
{
}
}
return "Hello World";
}
public static byte[] ReadStream(Stream input)
{
byte[] buffer = new byte[16 * 1024];
using (MemoryStream ms = new MemoryStream())
{
int read;
while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, read);
}
return ms.ToArray();
}
}
private Boolean InsertUpdateData(SqlCommand cmd)
{
String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["default"].ConnectionString;
SqlConnection con = new SqlConnection(strConnString);
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
try
{
con.Open();
cmd.ExecuteNonQuery();
return true;
}
catch (Exception ex)
{
//Response.Write(ex.Message);
return false;
}
finally
{
con.Close();
con.Dispose();
}
}
}
您可以添加在頁面加載時單擊的C#代碼,並單擊要上傳的按鈕嗎? –