通过传递Base 64的文件,使用REST odata服务上传大文件

本文关键字:文件 odata REST 服务 使用 Base | 更新日期: 2023-09-27 18:17:07

我想通过REST odata服务保存一个附件文件。我在服务中使用了实体框架。我将base 64的文件传递到rest客户机的主体中,如下所示:

服务调用url:

http://localhost:20347/NewServices.svc/Attc(DocEntry=831,Image='',Password='',PType='',UserID='')
XML:

<?xml version="1.0" encoding="utf-8"?><entry xml:base="http://localhost:20347/NewServices.svc/" xmlns="http://www.w3.org/2005/Atom" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"><id>http://localhost:20347/NewServices.svc/Attc(DocEntry=831,Image='',Password='',PType='',UserID='')</id><category term="Web_Services_Model.Attc" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" /><link rel="edit" title="Attc" href="Attc(DocEntry=831,Image='',Password='',PType='',UserID='')" /><title /><updated>2016-11-08T10:58:41Z</updated><author><name /></author><content type="application/xml"><m:properties><d:DocEntrym:type="Edm.Int32">831</d:DocEntry><d:DocNumm:type="Edm.Int32">830</d:DocNum><d:U_ID>PID1058</d:U_PID><d:UID>1</d:UID><d:Act>138</d:Act><d:PType></d:PType><d:Image>BASE 64</d:Image><d:UserID>admin</d:UserID><d:Password></d:Password></m:properties</content></entry>

服务中用于保存图像文件的代码。

[ChangeInterceptor("OPDAttc")]
    public void SaveAttc(OPDAttc Atch, UpdateOperations operations)
    {
        #region Add
        if (operations == UpdateOperations.Add)
        {using (Image image = Image.FromStream(new MemoryStream(Convert.FromBase64String(ImgtoSave))))
                        {

                            image.Save(FileName + imageid + "_" + time_now + "_IMG.jpg", ImageFormat.Jpeg);
                            FileName = FileName + imageid + "_" + time_now + "_IMG.jpg";
                        }}
        WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.BadRequest;
        WebOperationContext.Current.OutgoingResponse.StatusDescription = Result;
        #endregion
    }

我解码base64,转换回原始图像,然后保存在文件夹中。但我只能保存小文件。如果文件很大,则无法调用该服务,因此无法保存该文件。我如何使用此服务以正确的格式妥善保存任何类型的文件?通过这个服务保存附件文件(所有文件类型和大小)的最好和最简单的方法是什么?

通过传递Base 64的文件,使用REST odata服务上传大文件

我不太熟悉WCF,但我知道它的默认消息大小为64k。如果这就是您的问题,那么WCF -如何增加消息大小配额中的答案可能会对您有所帮助。