使用Multipart表单数据进行文件上载的Web服务 [英] Webservice for file upload using Multipart form data

查看:104
本文介绍了使用Multipart表单数据进行文件上载的Web服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我面临着如何使用多部分表单数据使用TransferFile()方法的Web服务的关键问题?

我已经为它编写了以下代码:

< pre lang =c#> [WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public void TransferFile()
{

HttpContext postedContext = HttpContext.Current;
HttpFileCollection Request = postingContext.Request.Files;
foreach (HttpPostedFile item in Request)
{
string filename = item.FileName;

byte [] fileBytes = new 字节 [item.ContentLength];
item.InputStream.Read(fileBytes, 0 ,item.ContentLength);
TransferFile(filename,fileBytes);

// objFileTransfer.TransferFile(filename,fileBytes);
retJSON = js.Serialize( new {Result = objFileTransfer.TransferFile(filename,fileBytes)});
Context.Response.Write(retJSON);
// fileBytes现在包含文件内容
// 文件名包含文件名
}
}



这个网络服务是否获得多部分表单数据?

当iPhone开发人员使用此API时,他们没有得到回复。





  private  FtpWebRequest ftpRequest =  null ; 
// private FtpWebResponse ftpResponse = null;
private 流ftpStream = null ;
private int bufferSize = 2048 ;

public string TransferFile( string filename, byte [] fileContent)
{
string strMsg = string .Empty;
尝试
{

/ * 创建FTP请求* /
字符串 uploadUrl = 字符串 .Format( {0} / {1} / {2},< span class =code-string>
ftp:// Address DirectoryName,filename);
ftpRequest =(FtpWebRequest)FtpWebRequest.Create(uploadUrl);
/ * 使用提供的用户名和密码登录FTP服务器* /
ftpRequest.Credentials = new NetworkCredential( 用户名 密码);
/ * 如有疑问,请使用以下选项* /
ftpRequest.UseBinary = true ;
ftpRequest.UsePassive = true ;
ftpRequest.KeepAlive = true ;
/ * 指定FTP请求的类型* /
ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;
尝试
{

ftpRequest.ContentLength = fileContent.Length;

流requestStream = ftpRequest.GetRequestStream();
requestStream.Write(fileContent, 0 ,fileContent.Length);
requestStream.Close();
FtpWebResponse response =(FtpWebResponse)ftpRequest.GetResponse();
strMsg = 文件上传状态: + response.ToString();

}
catch (Exception ex){Console.WriteLine(ex.ToString()); }
/ * 资源清理* /
ftpStream.Close();
ftpRequest = null ;
}
catch (Exception ex){Console.WriteLine(ex.ToString()); }
return strMsg;

}

解决方案

Hello Rajesh,



以下是示例代码。当点击提交按钮时,它会将所选文件数据发布到提到的(动作)网站。



 <  表单    id   =  Form1   < span class =code-attribute> action   =  htt://mysite.com/photo    方法  = < span class =code-keyword> post    enctype   =  multipart / form-data >  
< 输入 id = myPhoto name = myPhoto type = file / >
< input type = 提交 / >
< / form >



谢谢,

Imdadhusen


请检查答案 - WCF服务接受邮政编码的multipart / form-data [ ^ ]。



这可能对你有帮助。


I am facing critical problem that How to make webservice that uses TransferFile() method by multipart form data?.
I have write following code for it:

[WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public void TransferFile()
        {

            HttpContext postedContext = HttpContext.Current;
            HttpFileCollection Request = postedContext.Request.Files;
            foreach (HttpPostedFile item in Request)
            {
                string filename = item.FileName;

                byte[] fileBytes = new byte[item.ContentLength];
                item.InputStream.Read(fileBytes, 0, item.ContentLength);
                TransferFile(filename, fileBytes);

                //objFileTransfer.TransferFile(filename, fileBytes);
                retJSON = js.Serialize(new { Result = objFileTransfer.TransferFile(filename, fileBytes) });
                Context.Response.Write(retJSON);
                // fileBytes now contains the content of the file
                // filename contains the name of the file
            }            
        }


Is this webservice get multipart form data?
When an iPhone developer consume this api they are not getting response.


private FtpWebRequest ftpRequest = null;
        //private FtpWebResponse ftpResponse = null;
        private Stream ftpStream = null;
        private int bufferSize = 2048;

        public string TransferFile(string filename, byte[] fileContent)
        {
            string strMsg = string.Empty;
            try
            {
                
                /* Create an FTP Request */
                String uploadUrl = String.Format("{0}/{1}/{2}", "ftp://Address", "DirectoryName", filename);
                ftpRequest = (FtpWebRequest)FtpWebRequest.Create(uploadUrl);
                /* Log in to the FTP Server with the User Name and Password Provided */
                ftpRequest.Credentials = new NetworkCredential("Username", "Password");
                /* When in doubt, use these options */
                ftpRequest.UseBinary = true;
                ftpRequest.UsePassive = true;
                ftpRequest.KeepAlive = true;
                /* Specify the Type of FTP Request */
                ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;
                try
                {
                    
                    ftpRequest.ContentLength = fileContent.Length;

                    Stream requestStream = ftpRequest.GetRequestStream();
                    requestStream.Write(fileContent, 0, fileContent.Length);
                    requestStream.Close();
                    FtpWebResponse response = (FtpWebResponse)ftpRequest.GetResponse();
                    strMsg = "File Upload Status: " + response.ToString();
                    
                }
                catch (Exception ex) { Console.WriteLine(ex.ToString()); }
                /* Resource Cleanup */
                ftpStream.Close();
                ftpRequest = null;
            }
            catch (Exception ex) { Console.WriteLine(ex.ToString()); }
            return strMsg;
 
        }

解决方案

Hello Rajesh,

Following are sample code. When clicking on submit button it will post selected files data to the mentioned (action) website.

<form id="Form1" action="htt://mysite.com/photo" method="post" enctype="multipart/form-data">
     <input id="myPhoto" name="myPhoto" type="file" />
     <input type="submit" />
</form>


Thanks,
Imdadhusen


Please check the answer - WCF service to accept a post encoded multipart/form-data[^].

This might help you.


这篇关于使用Multipart表单数据进行文件上载的Web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆