在按钮上单击上传文件

时间:2012-06-18 04:15:05

标签: javascript file-upload windows-ce

我正在使用WinCE OS开发经典ASP。我想从WinCE上传文件并在本地机器上保存。请分享必要的JScript函数进行文件上传,我可以把它放在一个包含文件中。谢谢。

2 个答案:

答案 0 :(得分:0)

更好的方法是使用任何JavaScript库..比如jQuery ..

以下是文件上传示例..

http://pixelcone.com/jquery/ajax-file-upload-script/

How can I upload files asynchronously?

干杯:)

答案 1 :(得分:0)

我没有关于asp经典的信息,但是我已经使用了Asp.net,你可以使用asp来接收文件以便从wince上传文件使用可以使用c#开发app这里就是一个例子。

其客户端WinCE应用程序代码函数Upload(字符串Path,String FileName)将文件路径和文件名作为输入并将其发布到网页

#region Upload
public bool Upload(string FilePath, string FileName)
{
string Url = "HTTP://test.mtsonweb.com/fileupload.ashx"; //  Change it to your page name
string BytesConfirmedReceived = "";
int BytesSent = 0;
bool Ret = false;
ASCIIEncoding encoding = new ASCIIEncoding();
try
{

if (File.Exists(FilePath +"\\"+ FileName) == false) { return true; }

//FileInfo oInfo = new FileInfo(FilePath + "\\" + FileName);
//BytesSent = Convert.ToInt32(oInfo.Length.ToString());

Url += "?myfile=" + FileName.Trim();

FileStream fr = new FileStream(FilePath + "\\" + FileName, FileMode.Open);

BinaryReader r = new BinaryReader(fr);
byte[] FileContents = r.ReadBytes((int)fr.Length);
BytesSent = FileContents.Length;

r.Close();
fr.Close();

WebRequest oRequest = WebRequest.Create(Url);

oRequest.Method = "POST";

oRequest.Timeout = 15000;

oRequest.ContentLength = FileContents.Length;

Stream oStream = oRequest.GetRequestStream();

BinaryWriter oWriter = new BinaryWriter(oStream);

oWriter.Write(FileContents);

oWriter.Close();

oStream.Close();

WebResponse oResponse = oRequest.GetResponse();
BytesConfirmedReceived = new StreamReader(oResponse.GetResponseStream(),
Encoding.Default).ReadToEnd();

oResponse.Close();

if (BytesSent.ToString() == BytesConfirmedReceived.Trim())
{
Ret = true;
}

}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
return Ret;

}
#endregion

现在你的页面你可以处理使用脚本上传的文件,我已经使用了asp.net和c#作为后端,下面是页面的来源:

<%@ WebHandler Language="C#" Class="FileUpload" %>
using System;
using System.Xml;
using System.Data;
using System.Web;
using System.IO;
using System.Text;
using System.Runtime.InteropServices;
using System.Drawing;

public class FileUpload : IHttpHandler
{
public void ProcessRequest(HttpContext oContext)
{

int BytesSent = 0;
//string LocalPath = @"C:\Inetpub\wwwroot\";

string MyFile = "";


try
{

MyFile = oContext.Request["myfile"].ToString().Trim();
MyFile = HttpContext.Current.Server.MapPath("~/Demo/Files/" +

ASCIIEncoding encoding = new ASCIIEncoding();

BytesSent = oContext.Request.TotalBytes;

Class1 obj = Class1.GetInstance();

obj.FileName = MyFile;
obj.FileLength = BytesSent;

byte[] InComingBinaryArray =
oContext.Request.BinaryRead(oContext.Request.TotalBytes);
obj.Data = InComingBinaryArray;

if (File.Exists(MyFile) == true)
{
File.Delete(MyFile);
}

FileStream fs = new FileStream(MyFile, FileMode.CreateNew);
BinaryWriter w = new BinaryWriter(fs);
w.Write(InComingBinaryArray);
w.Close();
fs.Close();

FileInfo oInfo = new FileInfo(MyFile);
long a = (long)BytesSent;

oContext.Response.Write(oInfo.Length.ToString());

}
catch (Exception err) { oContext.Response.Write(err.Message); }

}

public bool IsReusable { get { return true; } }

}