Le 2011-05-03 à 12:35, Mike Schrag a écrit :
> if you can guarantee small data, and you have some level of confidence that
> your service can't be abused, file uploading into an NSData is maybe OK …
> otherwise you should be using the multipart request file upload API's in WO.
> You'll have to do a little plumbing yourself.
As (coming from one of Apple example apps):
WOMultipartIterator mpI = request().multipartIterator();
if (mpI == null) {
throw new IllegalStateException("Could not get the WOMultipartIterator.
Verify that the WOForm's 'enctype' binding is set to 'multipart/form-data'");
}
WOMultipartIterator.WOFormData wfd = null;
while (true) {
wfd = mpI.nextFormData();
if (wfd != null) break;
if
("thefile".equals(wfd.contentDispositionHeaders().valueForKey("name"))) break;
}
if ( (wfd != null) && (wfd.isFileUpload()) ) {
// Get the filename information from the request
NSDictionary contentDispHeaders = wfd.contentDispositionHeaders();
String aFileName = (String) contentDispHeaders.valueForKey("filename");
// Get just the name for the uploaded file from aFileName.
String fileName = null;
fileName = NSPathUtilities.lastPathComponent( aFileName );
String outputFilePath = new String("/tmp" + File.separator + fileName );
// Write the file out to the location
if ((fileName!=null) && (fileName.length()>0)) {
try {
InputStream wfdIs = wfd.formDataInputStream();
FileOutputStream fileOutputStream = new
FileOutputStream(outputFilePath);
// This will use a 1MB buffer to transfer chunks of data from the
Client to the Disk
int read = 0;
byte[] buffer = new byte[1024 * 1024]; // 1MB buffer
do {
read = wfdIs.read(buffer);
if (read != -1) {
fileOutputStream.write(buffer, 0, read);
} else {
break;
}
} while (read != -1);
fileOutputStream.flush();
fileOutputStream.close();
} catch (IOException e) {
NSLog.err.appendln("Error writing file: " + e);
}
} else {
// fileName == null or length 0
NSLog.out.appendln( "No File Uploaded" );
}
} else {
// wfd == null means that there was no file uploaded.
NSLog.out.appendln( "No File Uploaded" );
}
> ms
>
> On May 3, 2011, at 12:31 PM, Jesse Tayler wrote:
>
>> Mike -
>>
>> Oh, thanks, and I probably don't want to, but I didn't see the ERRestRoute I
>> kinda expected?
>>
>> I just put image data (NSData) into the form on my client app, and figured
>> I'd get it out of the form on the server.
>>
>> What's my best rout to implement this type of file upload, given that my
>> model and eo's work ok for the web client already.
>>
>> I'm just going from iphone to WO, as I'm sure many of us are.
>>
>>
>> On May 3, 2011, at 12:22 PM, Mike Schrag wrote:
>>
>>> write it to a file … you probably don't ever want file uploads into
>>> NSData's, btw.
>>>
>>> ms
>>>
>>> On May 3, 2011, at 12:17 PM, Jesse Tayler wrote:
>>>
>>>> Thanks Henrique,
>>>>
>>>> Ah, a processor makes sense. I use "S3" and I'm just getting Form-values
>>>> for the filename and nsdata.
>>>>
>>>> What I can see is an API with a java.io.File, and not the NSData
>>>> "posterData" which I got from the form values?
>>>>
>>>> NSData posterData =
>>>> (NSData)formValuesDictionary.valueForKeyPath("poster");
>>>> ERAttachment attachment =
>>>> ERAttachmentProcessor.processorForType("s3").process(editingContext(),
>>>> posterData, (String)dic.valueForKeyPath("filename"));
>>>>
>>>> Am I following you correctly in that I should use this S3 processor API ?
>>>> I guess I'd have to write the NSData to a temp file?
>>>>
>>>> Or is there an API I'm confusing which would handle the NSData I get from
>>>> the form?
>>>>
>>>>
>>>>
>>>>
>>>> On May 3, 2011, at 7:50 AM, Henrique Prange wrote:
>>>>
>>>>> Hi Jesse,
>>>>>
>>>>> ERAttachment is an abstract class. You can't instantiate it using the
>>>>> createERAttachment method.
>>>>>
>>>>> You should create the attachment using the ERAttachmentProcessor instead:
>>>>>
>>>>> ERAttachment attachment =
>>>>> ERAttachmentProcessor.processorForType(storageType).process(editingContext,
>>>>> uploadedFile, originalFileName, mimeType, configurationName, ownerID);
>>>>>
>>>>> Cheers,
>>>>>
>>>>> Henrique
>>>>>
>>>>> On 02/05/2011, at 15:30, Jesse Tayler wrote:
>>>>>
>>>>>> I'm trying to create an ERAttachment via ERRest,
>>>>>>
>>>>>> I didn't see an ERRestRoute for uploading file attachments but maybe
>>>>>> that's what I'm missing?
>>>>>>
>>>>>> I get a creation error while trying to create ERAttachment, and I
>>>>>> realize I likely should be creating the ERAttachmentData object first,
>>>>>> or more likely, find the ERRestRoute that's best for uploading a new
>>>>>> file attachment?
>>>>>>
>>>>>>
>>>>>>
>>>>>> NSData posterData = (NSData)dic.valueForKeyPath("poster");
>>>>>> ERAttachment attachment =
>>>>>> ERAttachment.createERAttachment(editingContext(), true, new
>>>>>> NSTimestamp(), (String)dic.valueForKeyPath("mimetype"),
>>>>>> (String)dic.valueForKeyPath("filename"), false, sizeInteger,
>>>>>> webPathString));
>>>>>> user().setPoster(attachment);
>>>>>>
>>>>>>
>>>>>>
>>>>>> _______________________________________________
>>>>>> Do not post admin requests to the list. They will be ignored.
>>>>>> Webobjects-dev mailing list ([email protected])
>>>>>> Help/Unsubscribe/Update your Subscription:
>>>>>> http://lists.apple.com/mailman/options/webobjects-dev/hprange%40gmail.com
>>>>>>
>>>>>> This email sent to [email protected]
>>>>>
>>>>> _______________________________________________
>>>>> Do not post admin requests to the list. They will be ignored.
>>>>> Webobjects-dev mailing list ([email protected])
>>>>> Help/Unsubscribe/Update your Subscription:
>>>>> http://lists.apple.com/mailman/options/webobjects-dev/jtayler%40oeinc.com
>>>>>
>>>>> This email sent to [email protected]
>>>>>
>>>>
>>>> _______________________________________________
>>>> Do not post admin requests to the list. They will be ignored.
>>>> Webobjects-dev mailing list ([email protected])
>>>> Help/Unsubscribe/Update your Subscription:
>>>> http://lists.apple.com/mailman/options/webobjects-dev/mschrag%40pobox.com
>>>>
>>>> This email sent to [email protected]
>>>
>>>
>>
>
> _______________________________________________
> Do not post admin requests to the list. They will be ignored.
> Webobjects-dev mailing list ([email protected])
> Help/Unsubscribe/Update your Subscription:
> http://lists.apple.com/mailman/options/webobjects-dev/probert%40macti.ca
>
> This email sent to [email protected]
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list ([email protected])
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/webobjects-dev/archive%40mail-archive.com
This email sent to [email protected]