i just want to save an uploaded file to a folder in server. My System design has UI and server part.The UI Is fully HTML and the server side is ASP.net. The data transfer is through json code.
Suppose i have an HTML file control in UI then how can i upload this to server.
Can anyone advise me with an example
code sample
$("#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);
}
});
});
in web service
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;
}
here in this code the "File_Doc" in service refers to the file upload control in asp.net. so how can ireplace it with html file control.
i got some code
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;
}
but I got an error in HttpPostedFile file = Request.Files["myFile"];
ie request does not exist in the current context