使用jquery-ajax上传文件,path以字符串形式

时间:2013-03-23 09:16:52

标签: asp.net json jquery c#-2.0

我正在使用C#开发ASP.net 2.0 Web应用程序。

我需要上传文件路径为string的文件。

cs文件中的Methos

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string SendContactUsEmail(string volume, ...., string strFileName, string message)
    {
    // Other parameter is removed in method and written ...
    // Need to upload file where path of the file is in string strFileName
    //rqContact.PakkagingfileName = $('#packagingFile').val(); = strFileName
    }    

从script.js传递的值如下:

if(reqType == '5')                                    
{   
    rqContact.height = $('#height').val();  
    rqContact.PakkagingfileName = $('#packagingFile').val();
}

$.ajax({                            
    type: 'POST',                       
    url: $('#webServiceUrl').val() + "/SendContactUsEmail",
    contentType:"multipart/form-data; charset=utf-8",
    //contentType:"application/json; charset=utf-8",                        
dataType: 'json',                           
    data: JSON.stringify(rqContact),  
    error: function(){                       
       //alert("Error in Contact-us Ajax call");
      } ,

     success: function( response )
       {                            
         trckContactUsEvent()
         if (form.find( '.actionurl' ).attr('name') == 'actionurlcontactus'){
        Cufon.refresh('.cufon-text');

        $('.resultContainer p').html(response);
        $('.resultContainer').attr('data-status','success');
        $('.result').show();
        $('.result').modal({
            opacity:70,
            overlayCss: {backgroundColor:'#000000'},
            containerId: 'simplemodal-container4',
            containerCss: {height:'48px'}
        });
       }
}
});

请告知如何上传文件。

问候。

1 个答案:

答案 0 :(得分:0)

您可以使用Ajax Upload上传文件。您必须为上传生成.ashx(常规处理程序)。您可以将文件的路径发送到处理程序并将文件保存到处理程序中的路径。这个Tutorial描述了如何使用Uploadify jquery插件上传文件。我不建议您使用Uploadify,但本教程可以帮助您学习如何使用General Handler。