我只想将上传的文件保存到服务器中的文件夹中。我的系统设计有 UI 和服务器部分。UI 完全是 HTML,服务器端是 ASP.net。数据传输是通过json代码。
假设我在 UI 中有一个 HTML 文件控件,那么我如何将它上传到服务器。
谁能给我举个例子
代码示例
$("#btn_AddDoc").click( function (e) {
e.preventDefault();
var InsDocDet = {};
InsDocDet.docname=$("#DocName").val();
InsDocDet.ownerUser=1;
InsDocDet.catid=$("#drp_cat").val();
InsDocDet.createDatetime=new Date();
InsDocDet.description_d=$("#Desc").val();
InsDocDet.comments_=$("#cmnts").val();
InsDocDet.deptid_=1;
InsDocDet.con_type=1;
InsDocDet.size_=1;
InsDocDet.Doc_status="up";
var fullPath =$("#doc_upload").val(); //document.getElementById('doc_upload').value;
if (fullPath) {
var startIndex = (fullPath.indexOf('\\') >= 0 ? fullPath.lastIndexOf('\\') : fullPath.lastIndexOf('/'));
var filename = fullPath.substring(startIndex);
if (filename.indexOf('\\') === 0 || filename.indexOf('/') === 0) {
filename = filename.substring(1);
InsDocDet.file_name=filename;
$.ajax({
type: "POST",
// <!-- url: "http://localhost/EMRDMSService/Service.asmx/User_Login",-->
url: "http://localhost/EMRDMSService/Service.asmx/srv_Insert_Document",
data: "{ins_Doc:" + JSON.stringify(InsDocDet) + "}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (r) {
console.log(r.d.STAT);
}
});
});
在网络服务中
public string srv_Insert_Document(insert_doc_details ins_Doc)
{
InsDoc_Ret doc_ret = new InsDoc_Ret();
try
{
string filename = Path.GetFileName(ins_Doc.file_name);
string path = Server.MapPath("~/");
File_Doc.SaveAs(Server.MapPath(Path.Combine("~/Files/" + dept_name + "/", filename)));
string ctype = File_Doc.PostedFile.ContentType;
int len = File_Doc.PostedFile.ContentLength;
double size = len / 1024;
string sizkb = Convert.ToString(size) + " KB";
string fname = dept_name + "/" + filename;
int doc_id = newdb_class.Insert_Data_Scaler("INSERT INTO dms_document_details (DOC_NAME,FILE_NAME,OWNER_USER,CAT_ID,CREATE_DATE,DESCRIPTION,COMMENTS,DEPT_ID,contype,Size,DocStatus) VALUES ('" + ins_Doc.docname + "','" + ins_Doc.file_name + "','" + ins_Doc.ownerUser + "','" + ins_Doc.catid + "',NOW(),'" + ins_Doc.description_d + "','" + ins_Doc.comments_ + "','" + ins_Doc.deptid_ + "','" + ins_Doc.con_type + "','" + ins_Doc.size_ + "','" + ins_Doc.Doc_status + "')");
doc_ret.SID = "1";
doc_ret.STAT="SUCCESS";
}
catch (Exception ex)
{
doc_ret.SID = "1";
doc_ret.STAT = "FAIL";
}
JavaScriptSerializer js = new JavaScriptSerializer();
string strJSON = js.Serialize(doc_ret);
return strJSON;
}
在此代码中,服务中的“File_Doc”指的是 asp.net 中的文件上传控件。那么如何用html文件控件替换它。
我有一些代码
public string srv_Insert_Document(insert_doc_details ins_Doc)
{
InsDoc_Ret doc_ret = new InsDoc_Ret();
try
{
DataTable dt = new DataTable();
dt = newdb_class.Read_Data("SELECT Dept_Name FROM dms_dept_details WHERE Dept_id='" + ins_Doc.deptid_ + "'");
string dept_name = dt.Rows[0][0].ToString().Trim();
string filename = Path.GetFileName(ins_Doc.file_name);
string path = Server.MapPath("~/");
HttpPostedFile file = Request.Files["myFile"];
if (file != null && file.ContentLength > 0)
{
string fname = Path.GetFileName(file.FileName);
file.SaveAs(Server.MapPath(Path.Combine("~/Files/" + dept_name + "/", filename)));
}
string ctype = file.ContentType;
int len = file.ContentLength;
double size = len / 1024;
string sizkb = Convert.ToString(size) + " KB";
string fname_ins = dept_name + "/" + filename;
int doc_id = newdb_class.Insert_Data_Scaler("INSERT INTO dms_document_details (DOC_NAME,FILE_NAME,OWNER_USER,CAT_ID,CREATE_DATE,DESCRIPTION,COMMENTS,DEPT_ID,contype,Size,DocStatus) VALUES ('" + ins_Doc.docname + "','" + ins_Doc.file_name + "','" + ins_Doc.ownerUser + "','" + ins_Doc.catid + "',NOW(),'" + ins_Doc.description_d + "','" + ins_Doc.comments_ + "','" + ins_Doc.deptid_ + "','" + ins_Doc.con_type + "','" + ins_Doc.size_ + "','" + ins_Doc.Doc_status + "')");
doc_ret.SID = "1";
doc_ret.STAT="SUCCESS";
}
catch (Exception ex)
{
doc_ret.SID = "1";
doc_ret.STAT = "FAIL";
}
JavaScriptSerializer js = new JavaScriptSerializer();
string strJSON = js.Serialize(doc_ret);
return strJSON;
}
但我有一个错误HttpPostedFile file = Request.Files["myFile"];
即当前上下文中不存在请求