在 asp 中的按钮单击上上传文件
Upload a file on Button click in asp
我正在使用WinCE OS开发经典ASP。我想从 WinCE 上传文件并保存在本地计算机中。请分享文件上传所需的 JScript 函数,我可以将其放在包含文件中。谢谢。
更好的方法是使用任何JavaScript库,如jQuery。
以下是文件上传示例。
http://pixelcone.com/jquery/ajax-file-upload-script/
如何异步上传文件?
干杯:)
我没有关于asp classic的信息,但我使用了 Asp.net,您可以使用asp接收文件,以便从wince上传文件,使用可以使用C#开发应用程序,这是一个示例。
其客户端WinCE应用代码函数上传(字符串路径,字符串文件名)将文件路径和文件名作为输入,并将其发布到网页
#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; } }
}
相关文章:
- 在单击href链接的同时下载文件
- 单击按钮更改加载到表中的JSON文件
- 在Node Webkit应用程序中从DOM单击按钮时运行批处理文件
- 打开'选择文件'窗口单击锚链接(跳过单击'选择文件'按钮)
- 创建一个HTML文件,该文件使用DOM在用户每次单击按钮时交换两段内容
- 如何在单击按钮时编辑文件的内容?PHP
- 在播放完成之前再次单击播放音频文件
- 选择文件(表单提交)后无法单击“取消”按钮
- Javascript 单击html字段值而不编辑.js文件
- 我想通过单击按钮将 2 个 php 文件链接在一起
- 通过 NG 单击选择文件或文本区域数据
- Javascript不起作用,但当放置断点并从chrome调试器工具中单击文件时,它可以正常工作
- Javascript自动播放声音文件,单击其中一个按钮时停止
- 单击跨度内的隐藏文件输入会导致RangeError
- 单击页面中的任何按钮时,使用Jquery file Upload时都会打开文件对话框
- 文件上传don'单击链接按钮时无法打开
- 单击按钮时使用JS读取JSON文件
- 如何在单选按钮上更新XML文件单击(客户端)c# Web应用程序
- 代码不能很好地执行PHP文件单击一个按钮
- <输入类型=“文件” /单击“取消”时>更改属性