Javascript 在asp中单击按钮上载文件
我正在使用WinCE操作系统开发经典ASP。我想从WinCE上传一个文件并保存在本地机器中。请共享文件上传所需的JScript函数,我可以将其放入包含文件中。谢谢。更好的方法是使用任何JavaScript库。。比如jQuery 以下是文件上载示例Javascript 在asp中单击按钮上载文件,javascript,file-upload,windows-ce,Javascript,File Upload,Windows Ce,我正在使用WinCE操作系统开发经典ASP。我想从WinCE上传一个文件并保存在本地机器中。请共享文件上传所需的JScript函数,我可以将其放入包含文件中。谢谢。更好的方法是使用任何JavaScript库。。比如jQuery 以下是文件上载示例 干杯:我没有关于asp classic的信息,但我使用了asp.net,您可以使用asp接收文件,以便从wince上传文件。使用c可以开发应用程序。这里是一个示例 它的客户端WinCE应用程序代码函数Uploadstring Path,String F
干杯:我没有关于asp classic的信息,但我使用了asp.net,您可以使用asp接收文件,以便从wince上传文件。使用c可以开发应用程序。这里是一个示例 它的客户端WinCE应用程序代码函数Uploadstring 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; } }
}
谢谢你提醒德里克,我已经接受了答案。现在请帮我上传文件。我对问题进行了升级,这样更多的人可以帮助你。我不能使用JQuery插件,因为它来自WinCE操作系统和经典ASP。请建议另一种方法。你需要纯javascript解决方案吗?这是一种老方法。。但看看它是否能起作用