2013-04-13 2 views
0

그냥 업로드 된 파일을 서버의 폴더에 저장하고 싶습니다. 내 시스템 디자인 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); 
       } 
      }); 
     }); 

서비스에서이 코드 "File_Doc"여기
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; 

} 

파일 업로드 컨트롤을 말한다 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; 

} 

을 가지고 있지만 즉, 요청이 현재 컨텍스트에 존재하지 않는이

+0

답을 얻기 위해 더 쉬울 것이다 코드 몇 개를 게시하면 ... –

+0

http://bit.ly/17xgjIl –

+0

@code 라이더 -이 문제를 고집했습니다. 즉, 정확히 무엇을 해야할지 모르겠다. 음 ... uhave 경우 코드 샘플을 제공하십시오. – Sivajith

답변

1

파일을 업로드 번호 전체 필요

HttpPostedFile file = Request.Files["myFile"];에 오류가 발생했습니다 다시 게시.

숨겨진 iFrame에 대한 아약스 호출 게시를 시뮬레이트하여 페이지가 변경되지 않도록 할 수 있습니다. 그런 다음 jquery/javascript를 사용하여 iFrame의 내용을 확인하면 업로드가 성공적 이었는지를 볼 수 있으며 화면의 특정 부분을 새로 고칩니다. 당신은 ASP.NET MVC3 또는 여기 MVC4를 사용하는 경우

은 예입니다 http://www.dustinhorne.com/post/2011/11/16/AJAX-File-Uploads-with-jQuery-and-MVC-3.aspx

당신은 JS에서 일을 좀 더 일반적인 방법하려면 : http://tips.itliveweb.com/ajax/upload-image-using-iframe-javascript-ajax.html

+0

을 제공 할 수 있습니까? 예를 들어 html 컨트롤을 업로드 한 다음 임시 경로에 저장할 수 있습니까? – Sivajith

+0

예, 어딘가에 저장해야합니다. –

+0

그래서 내가 뭔가를 탐색 할 때 그것을 저장해야합니다. 좋아요? – Sivajith

관련 문제